Skip to main content

Command Palette

Search for a command to run...

LeetCode 49: Group Anagrams

LeetCode 49 Explained: Techniques to Group Anagrams

Updated
3 min read
LeetCode 49: Group Anagrams
V

I'm Varchasv, a Data Engineer working on enterprise data integration.

Currently on a 90-problem challenge to level up my technical skills and switch to a more development-focused role.

What I'm doing:

  • Solving 2 Leetcode problems daily (SQL + DSA).
  • Blogging about each problem.
  • Building in public.

My Goal - Land a better data engineering role by mid-2026.

Follow my journey !!

Date: January 16, 2026
Category: Array | HashMap | String | Sorting
Time Taken: 25 minutes
Difficulty: Medium


Problem Statement

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

Link: Group Anagrams


My Approach

Initial thought:

  • We can create a HashMap where each value is a list containing all the strings with the same characters. For the HashMap key, we can use the sorted version of the string and we can just append the list whenever we find a string matching the sorted key.

  • This solution will have time complexity of **O(N K logK)**, where K is the maximum length of a string and the space complexity will be O(N * K).

  • This approach works well when strings can contain any characters, not just a-z.

Final solution:
The most “optimal” solution would be to count the total number of characters in a string and use them as the key in the HashMap.

  • First create a HashMap of lists using defaultdict(list) .

  • Use a for loop to get to each string and define a list charFreq which will have the frequencies of each character in it.

  • Convert this charFreq to a tuple and use it as a key for our HashMap.


Solution Code (Sorted String method)

class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        # hashmap['aet'] = ['eat', 'tea', 'ate']
        # Creating a hashmap with list as it's values.
        anagrams = defaultdict(list)

        for word in strs:
        # Since sorted(str) returns a sorted list, we need to convert it back to a string
        # Because a list cannot be used as a key for a hashmap, or we could convert it to a 
        # tuple
            sword = ''.join(sorted(word))
            anagrams[sword].append(word)

        # Since we require a list and not dict_values() as the output.
        return list(anagrams.values())

Solution Code (Count Freq method)

class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        anagrams = defaultdict(list)

        for word in strs:
            # Create a list of length 26, with each value as 0
            charFreq = [0] * 26

            for char in word:
                # Add the frequency of every char in the word to charFreq
                charFreq[ord(char) - ord('a')] += 1

            # Store the charFreq list as a tuple to use it as the key and append the word for the freq
            anagrams[tuple(charFreq)].append(word)

        return list(anagrams.values())

Complexity:

Time: O(N*K) Space: O(N*K), where K is the longest word.

Key Takeaway: We can use ord() to get the value of every char from a-z.

Pattern: Frequency Array

Mistakes I Made:

  • I didn't remember how to define a dictionary with lists as values.

  • I forgot how ord() works in Python and its usage.

Series: 90 Days of Data Engineering Progress: 8/90 problems completed

Tags: #DEQuest #LeetCode #SQL #DataEngineering #BuildInPublic

90 Days of Data Engineering

Part 9 of 48

Solving 90 problems over 18 weeks. Daily posts Mon-Fri. Goal: Switch to development role.

Up next

LeetCode 183: Customers Who Never Order

LeetCode 183: Customers Who Never Order.