Group Anagrams - 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.

Input & Output

Example 1 — Basic Anagram Grouping
$ Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
💡 Note: eat, tea, ate contain same letters (a,e,t). tan, nat contain same letters (a,n,t). bat stands alone with letters (a,b,t).
Example 2 — Empty String
$ Input: strs = [""]
Output: [[""]]
💡 Note: Single empty string forms one group by itself.
Example 3 — Single Character
$ Input: strs = ["a"]
Output: [["a"]]
💡 Note: Single string with one character forms one group.

Constraints

  • 1 ≤ strs.length ≤ 104
  • 0 ≤ strs[i].length ≤ 100
  • strs[i] consists of lowercase English letters only

Visualization

Tap to expand
Group Anagrams: Organize Words by Same LettersINPUT ARRAYeatteatanatenatbat⬇ Find words with same letters ⬇GROUP 1: a,e,t lettersGROUP 2: a,n,t letters[eat, tea, ate]Same letters: a,e,t[tan, nat]Same letters: a,n,t[bat]Unique letters: a,b,tResult: [[eat,tea,ate], [tan,nat], [bat]]
Understanding the Visualization
1
Input
Array of strings that may be anagrams of each other
2
Process
Identify which strings are anagrams (same letters, different order)
3
Output
Groups of anagram strings in nested arrays
Key Takeaway
🎯 Key Insight: Anagrams have identical characters - sort each string's characters to create matching keys for grouping
Asked in
Amazon 85 Facebook 72 Microsoft 68 Google 54
270.6K Views
High Frequency
~15 min Avg. Time
8.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen