← Core Algorithms
#6 most askedVery commonMediumHash map

Group Anagrams

New to Hash map? Learn it from scratch first
Hash Maps & Sets β†’
🏒 Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.
I was asked this

Group words that are anagrams of each other (same letters, different order). Return the groups in any order.

["eat", "tea", "tan", "ate", "nat", "bat"] β†’ [["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]

πŸ§ͺ Practice

Write your solution, then press Run tests. Explain your approach out loud as you go.

Python
def group_anagrams(words):
    pass

Output appears here. Press Run or Ctrl/⌘ + Enter.

Try it first. Struggling for a few minutes is how it sticks.