Sorting each string takes O(K log K) time, where K is the length of the string. Doing this for all N strings results in O(NK log K) time complexity.
The “Group Anagrams” problem asks you to group strings that are anagrams of each other. Two strings are anagrams if they contain the same characters in any order, with the same frequency.
For example, given the input ["eat", "tea", "tan", "ate", "nat", "bat"]
, the output should be grouped as:
["eat", "tea", "ate"]
["tan", "nat"]
["bat"]
This problem is widely used to teach the power of hashing and the idea of transforming complex objects into simpler keys. It sharpens your understanding of how to efficiently group data based on custom equivalence logic and is foundational for problems involving classification, normalization, or frequency-based analysis.
The simplest way to group anagrams is by sorting each word and using the sorted result as the key. All anagrams, once sorted, will yield the same string. For example:
"eat" → "aet"
"tea" → "aet"
"ate" → "aet"
These will all be grouped under the key "aet"
.
anagramGroups
.
Sorting each string costs O(K log K)
, which can be slow for large K. Instead, we can use a frequency count of characters (like a 26-element array for lowercase English letters).
anagramGroups
.
Instead of sorting, counting characters takes O(K)
per word. This reduces the overall time complexity from O(NK log K)
to O(NK)
, where N is the number of words.
Input: ["eat", "tea", "tan", "ate", "nat", "bat"]
Using sorted strings:
"eat" → "aet"
"tea" → "aet"
"tan" → "ant"
"ate" → "aet"
"nat" → "ant"
"bat" → "abt"
Final groups:
["eat", "tea", "ate"]
["tan", "nat"]
["bat"]
Time Complexity:
O(NK log K)
O(NK)
[]
The “Group Anagrams” problem is a powerful example of classification and grouping using hash maps. It reinforces strategies like character counting and normalization, which are frequently useful in both algorithm design and data preprocessing tasks in the real world.