Group Anagrams
- Array
- String
- Hash Map
- Frequency Counting
Given an array of lowercase English strings, group all anagrams together.
For deterministic output, sort the strings inside each group lexicographically. Then sort the groups by the first string in each group.
Three balanced groups
- Input
"strs": ["loop", "pool", "node", "done", "listen", "silent"]
- Output
[ ["done", "node"], ["listen", "silent"], ["loop", "pool"] ]
Explanation: Each pair shares one character-frequency signature, and the groups follow the required lexical order.
Different word lengths
- Input
"strs": ["arc", "car", "elbow", "below", "state", "taste"]
- Output
[ ["arc", "car"], ["below", "elbow"], ["state", "taste"] ]
Explanation: Only words with identical letters and counts are placed together.
Constraints
- 1 <= strs.length <= 10,000
- 0 <= strs[i].length <= 100
- strs[i] contains lowercase English letters only
Follow-up
Can you build each grouping key in O(k) time for a string of length k without sorting the string?