Group Anagrams
Given an array of strings strs
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Example 2
Output: [[""]]
Example 3
Output: [["a"]]
Contents
Solution using a HashMap to store count of character appearance
In this approach, we are going to loop through every string in input strings array strskeyvalueList
For example if the input strings array is strs= ["ate", "eat"]
-
We will first initialize a
to store the results.HashMapJavaHashMap<HashMap<Character, Integer>, List<String>> result = new HashMap<>(); -
In the first step, we go through the characters in string "ate" and prepare a
where key is the character and number of times it appeared is the value.HashMap<Character, Integer>
a-1
t-1
e-1
we will store thiskey and the string asHashMap in avalue intoListresult .HashMap -
In the next step, we go through the characters in string "eat" and prepare a
where key is the character and number of times it appeared is the value, same as what we have done in above stepHashMap<Character, Integer>
e-1
a-1
t-1
Now, this key already present inresult , so we simply append it to theHashMapvalue .List -
Finally, we simply return all the values in the
result .HashMap
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class GroupAnagrams {
/* Using char -> count as key and value as List of strings, then group all strings who has same char -> count*/
static List<List<String>> usingHashMapToStoreCharacterCount(String[] strs) {
HashMap<HashMap<Character, Integer>, List<String>> result = new HashMap<>();
for(String str: strs) {
HashMap<Character, Integer> key = new HashMap<>();
for(char c: str.toCharArray()) {
key.merge(c, 1, Integer::sum);
}
result.merge(key, new ArrayList<>(List.of(str)), (oldVal, newVal) -> {
oldVal.addAll(newVal);
return oldVal;
});
}
return new ArrayList<>(result.values());
}
}
Complexity Analysis:
Time complexity: Above code runs in O(n * m) time where nstrsm
Space complexity: O(n) since we are storing all input strings into a HashMap
Solution using an array to store count of character appearance
This approach will be same as above one, the only differece is that, instead of using a HashMapchar
To get character filled into this array of size 26, we can get the position by doing character at position i - 'a'
Once the charStringString.valueOf(char[] chars)key
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class GroupAnagrams {
/* This is same as above, instead of using HashMap as key, we will construct an array with char -> count
and convert that to a string for e.g. abc will be [1, 1, 1, 0, 0, 0, 0, .........], it will become a
string with 26 characters length 11100000000000....*/
static List<List<String>> usingCharArrayAsKey(String[] strs) {
HashMap<String, List<String>> result = new HashMap<>();
for (String str : strs) {
char[] count = new char[26];
for (char c : str.toCharArray()) {
count[c - 'a']++;
}
result.merge(String.valueOf(count), new ArrayList<>(List.of(str)), (oldVal, newVal) -> {
oldVal.addAll(newVal);
return oldVal;
});
}
return new ArrayList<>(result.values());
}
}
Complexity Analysis:
Time complexity: Above code runs in O(n * m) time where nstrsm
Space complexity: O(n) since we are storing all input strings into a HashMap
Above implementations source code can be found at