Skip to main content

49. Group Anagrams

mediumAsked at Amazon

Given a list of strings, group anagrams together. Amazon asks this to test whether you reach for the 'sorted-string as key' or 'frequency-tuple as key' pattern to avoid pairwise anagram-checking.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Amazon loops.

  • Glassdoor (2026-Q1)Amazon SDE I/II onsite reports cite this as a hash-map staple.
  • Blind (2025-11)Recurring Amazon interview problem.

Problem

Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Constraints

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

Examples

Example 1

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

Example 2

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

Approaches

1. Sorted-string as hash key

For each string, sort its chars; group by sorted form.

Time
O(N * L log L)
Space
O(N * L)
function groupAnagramsSort(strs) {
  const groups = new Map();
  for (const s of strs) {
    const key = s.split('').sort().join('');
    if (!groups.has(key)) groups.set(key, []);
    groups.get(key).push(s);
  }
  return [...groups.values()];
}

Tradeoff: Common interview answer. Sort dominates per string.

2. Char-frequency tuple as key (optimal)

Count each char's occurrences. Use the count-vector as key.

Time
O(N * L)
Space
O(N * L)
function groupAnagrams(strs) {
  const groups = new Map();
  for (const s of strs) {
    const counts = new Array(26).fill(0);
    for (const ch of s) counts[ch.charCodeAt(0) - 97]++;
    const key = counts.join(',');
    if (!groups.has(key)) groups.set(key, []);
    groups.get(key).push(s);
  }
  return [...groups.values()];
}

Tradeoff: True O(N * L) — avoids the L log L sort. Marginal at L = 100 but worth mentioning as the optimization.

Amazon-specific tips

Amazon interviewers grade on whether you reach for the hash-key pattern. State out loud: 'Two strings are anagrams iff they have identical char frequencies. So I'll use a canonical form — either sorted string or frequency tuple — as the hash key.' Both versions score well; frequency tuple is the senior-IC signal.

Common mistakes

  • Comparing each pair via anagram check — O(N^2 * L), unnecessarily slow.
  • Using a Set of strings as the value instead of an array (loses order; problem doesn't require but cleaner to use arrays).
  • Forgetting to handle the empty string (it has its own canonical form '').

Follow-up questions

An interviewer at Amazon may pivot to one of these next:

  • Valid Anagram (LC 242) — the underlying check.
  • What if strings could contain unicode?
  • What if the alphabet were huge but strings were short?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Sort or frequency — which does Amazon prefer?

Both are accepted. Frequency is technically faster but at L = 100 the constant factors matter. Sort version is shorter to write.

Why is the frequency tuple a valid hash key?

Two anagrams have identical frequency tuples; non-anagrams have different ones. Joining the 26 counts with a delimiter gives a unique string for each frequency pattern.

Practice these live with InterviewChamp.AI

Drill Group Anagrams and other Amazon interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →