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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code