Skip to content

Practice Group Anagrams

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#49
LeetCode Problem

Group Anagrams

Problem Statement

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

Source: Group Anagrams on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
strs = ["eat","tea","tan","ate","nat","bat"]
Output
[["bat"],["nat","tan"],["ate","eat","tea"]]

# Example 2

Input
strs = [""]
Output
[[""]]

# Example 3

Input
strs = ["a"]
Output
[["a"]]

Constraints

1 <= strs.length <= 10^4
0 <= strs[i].length <= 100
strs[i] consists of lowercase English letters.

How to solve Group Anagrams

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

Two strings are anagrams exactly when they share a canonical form, either their letters in sorted order or a 26-slot letter-count signature. That form works as a hash key, so one pass over the input groups everything.

Why brute force is not enough

Compare every pair of strings with a letter-count test and union the matches into groups. With strs.length up to 10^4 that is roughly 5 * 10^7 pairs, each costing up to 100 characters, plus awkward bookkeeping to turn pairwise matches into groups.

Approach

  1. Choose the canonical key. Sorting the characters is simplest; a fixed array of 26 counts serialized with separators is faster because the alphabet is lowercase only.
  2. Walk the input once. For each string compute its key and append the original string to the list stored under that key in a hash map. Invariant: after i strings, every list in the map holds exactly the anagrams seen so far among those i strings.
  3. Return the map's values. Order within and across groups is free, so no post-processing is needed.

Walkthrough of Example 1

strs = ["eat","tea","tan","ate","nat","bat"] → [["bat"],["nat","tan"],["ate","eat","tea"]]

  1. The string eat sorts to aet; create the group aet holding eat.
  2. tea also sorts to aet; the group becomes eat, tea.
  3. tan sorts to ant, a new group; ate sorts to aet and joins eat, tea, ate.
  4. nat sorts to ant and joins tan; bat sorts to abt and starts its own group.
  5. Three groups remain: bat alone, tan with nat, and eat with tea and ate, matching the expected output since any order is accepted.

Time and space

Time O(N * K) · Space O(N * K). N is the number of strings (up to 10^4) and K the longest string (up to 100); counting keys cost O(K) each, sorting keys O(K log K). The map stores every input string plus one key per group.

Interview pitfalls

  • Concatenating counts without a delimiter, so a count of 1 followed by 11 collides with 11 followed by 1.
  • Using the sum of character codes as the key, which collides (ad and bc), or a product of primes, which overflows at length 100.
  • Dropping the empty string case: an input of one empty string must return one group containing it.
  • Using a mutable array as the map key in languages where such keys hash by identity rather than content.
  • Deduplicating identical strings; two copies of eat must both appear in the same group.

Follow-up questions interviewers ask

  • How would you choose the key if strings could include uppercase letters, digits, or arbitrary Unicode?
  • Given one long text and a short pattern, find every substring that is an anagram of the pattern in linear time.
  • If strings arrive as a stream too large for one machine, how would you partition the work so anagrams always land together?

Further reading on the technique: Hash table

Related LeetCode Problems

Practice more problems similar to Group Anagrams.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.