You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If 2 strings are sorted, and their sorted values are equal, they are anagrams.
Create a HashMap:
key - the sorted version of the String.
value - all the unsorted Strings that sort to the key.
Code
classSolution {
publicList<List<String>> groupAnagrams(String[] array) {
if (array == null || array.length == 0) {
returnnewArrayList();
}
Map<String, List<String>> map = newHashMap();
for (Stringstr : array) {
Stringkey = sortChars(str);
map.putIfAbsent(key, newArrayList<String>());
List<String> anagrams = map.get(key);
anagrams.add(str);
}
returnnewArrayList(map.values());
}
privateStringsortChars(Stringstr) {
char[] content = str.toCharArray(); // Strings are immutable, so we convert to char[]Arrays.sort(content);
returnnewString(content);
}
}
Time/Space Complexity
Time Complexity: O(nk log k), where n is the number of strings, and k is the length of the longest string.
Space Complexity: O(nk) for storing the strings.
Solution 2 - Character Counts
Algorithm
In our above solution, we were using sorting (O(n log n)) to see if 2 strings are anagrams. Instead, we can compare the letter counts of the Strings to see if they're anagrams (O(n))
The only difference in this solution is how we create the key for our HashMap. Our key will represent the number of letters in each string, where each letter is associated with a count. We can use a Map<Character, Integer> to represent this key.
If you don't like using a Map<Character, Integer> as a key, you can convert this to a String, using a separator (such as a hyphen -, or any non-digit character) between the counts. So bad would be 1-1-0-1-...0-, where the ... are the remaining 21 letters.
Time/Space Complexity
Time Complexity: O(nk), where n is the number of strings, and k is the length of the longest string.