Group Anagrams - Leetcode Solution


Step-by-Step Thought Process

Brute Force

  1. Initialize a dictionary to group anagrams.
  2. For each string in the input list:
    • Sort the characters in the string.
    • Use the sorted string as a key in the dictionary.
    • Append the original string to the list corresponding to this key.
  3. Return the values of the dictionary as the result.

Code Solution (Brute Force)


                

                

                

                

Inefficiency

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.

Optimal Approach

  1. Use a frequency count of letters (array of size 26) as the key instead of sorting.
  2. For each string in the input list:
    • Count the frequency of each letter (O(K) time).
    • Use the tuple of counts as a key in the dictionary.
    • Append the original string to the list corresponding to this key.
  3. Return the values of the dictionary as the result.
  4. Efficiency Gain Avoids the need to sort each string. The time complexity improves to O(NK), where N is the number of strings and K is the maximum length of a string.

Code Solution (Optimal)


                

                

                

                

Detailed Explanation

Understanding the Problem: Group Anagrams

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"]

Why This Problem Matters

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.

Basic Approach: Sort and Group

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".

Steps:

  1. Initialize an empty dictionary anagramGroups.
  2. For each word in the input list:
    • Sort the characters in the word (O(K log K), where K is the word length).
    • Use the sorted string as the key in the dictionary.
    • Append the original word to the value list of that key.
  3. Return all value lists from the dictionary.

Optimization: Use Letter Frequency Instead of Sorting

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).

Steps:

  1. Initialize an empty dictionary anagramGroups.
  2. For each string:
    • Create a frequency count (array of 26 zeros).
    • Increment the count for each character in the word.
    • Convert the array to a tuple (since lists aren't hashable) and use that as the key.
    • Append the word to the group corresponding to that key.
  3. Return all grouped values.

Efficiency Gain:

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.

Example Walkthrough

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 and Space Complexity

Time Complexity:

  • Using sorting: O(NK log K)
  • Using frequency counting: O(NK)
Space Complexity: O(NK), since we store N strings each of length up to K, and the dictionary stores grouping information.

Edge Cases to Consider

  • Empty input list → return []
  • All strings are identical → one group
  • All strings are unique with no anagrams → each string in its own group

Conclusion

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.

Get Personalized Support at AlgoMap Bootcamp 💡