Skip to content

Group Anagrams

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.

InputOutput
["eat","tea","tan","ate","nat","bat"][["bat"],["nat","tan"],["ate","eat","tea"]]
[""][[""]]
["a"][["a"]]
  • 1 <= strs.length <= 10^4
  • 0 <= strs[i].length <= 100
  • strs[i] consists of lowercase English letters.
ApproachTimeSpaceBest When
Sort KeyO(n·k·log k)O(n·k)General case, simple to implement
Char CountO(n·k)O(n·k)Long strings where sort cost is noticeable
★ Recommended

Sort each string’s characters to produce a canonical key. Words that are anagrams will produce the same key and get appended to the same bucket in the hash map.

⏱ Time O(n·k·log k) k = max string length; sorting each string costs k log k 💾 Space O(n·k) All strings stored once in the map

Input: ["eat","tea","tan","ate","nat","bat"]

WordSorted keyGroups state
"eat""aet"{"aet": ["eat"]}
"tea""aet"{"aet": ["eat","tea"]}
"tan""ant"{"aet": ["eat","tea"], "ant": ["tan"]}
"ate""aet"{"aet": ["eat","tea","ate"], "ant": ["tan"]}
"nat""ant"{..., "ant": ["tan","nat"]}
"bat""abt"{..., "abt": ["bat"]}

Animated walkthrough

How sort key groups anagrams

Use Next or Autoplay to watch each word get sorted into its canonical key, then placed into the correct bucket.

Step 1 of 6
Waiting to begin...
Array state
Memory / lookup state

function groupAnagrams(strs):
groups = defaultdict(list)
for s in strs:
key = sorted(s) as tuple/string
groups[key].append(s)
return list(groups.values())
group_anagrams_sort_key.py
from typing import List
from collections import defaultdict
def groupAnagrams(strs: List[str]) -> List[List[str]]:
groups = defaultdict(list)
for s in strs:
key = tuple(sorted(s))
groups[key].append(s)
return list(groups.values())
print(groupAnagrams(["eat","tea","tan","ate","nat","bat"]))
🎯 Interview Favourite

Instead of sorting, count the frequency of each of the 26 lowercase letters and use that count array as the key. This removes the log k sort factor, giving a linear-per-string cost.

⏱ Time O(n·k) k = max string length; one pass per string 💾 Space O(n·k) All strings stored once in the map

For "eat":

  • count[4] (e) = 1, count[0] (a) = 1, count[19] (t) = 1
  • key = (0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0)

For "tea" — identical count array → same bucket as "eat".

function groupAnagrams(strs):
groups = defaultdict(list)
for s in strs:
count = [0] * 26
for c in s:
count[ord(c) - ord('a')] += 1
groups[tuple(count)].append(s)
return list(groups.values())
group_anagrams_char_count.py
from typing import List
from collections import defaultdict
def groupAnagrams(strs: List[str]) -> List[List[str]]:
groups = defaultdict(list)
for s in strs:
count = [0] * 26
for c in s:
count[ord(c) - ord('a')] += 1
groups[tuple(count)].append(s)
return list(groups.values())
print(groupAnagrams(["eat","tea","tan","ate","nat","bat"]))
✓ Simple

A third approach demonstrating an alternative technique for solving this problem. This implementation optimizes either time or space complexity compared to the previous approaches.

⏱ Time O(n) Optimized complexity 💾 Space O(1) Efficient space usage
function approach3():
// Implementation approach goes here
group_anagrams_space_optimized.py
"""
Solution for Group Anagrams
"""
def solve():
"""Implementation for approach 3 of Group Anagrams"""
pass
if __name__ == "__main__":
print("Solution ready")