302. Group Anagrams
Medium
Canonical Hash Key
Hashing
Problem
Given an array of strings strs, group the anagrams together. Strings belong to the same group when they contain exactly the same characters with the same frequencies. The groups and the strings inside each group may be returned in any order.
Examples
Example 1
Input: ["eat","tea","tan","ate","nat","bat"]
Output: [["eat","tea","ate"],["tan","nat"],["bat"]]
Example 2
Input: [""]
Output: [[""]]
Example 3
Input: ["a"]
Output: [["a"]]
Constraints
� 1 <= strs.length <= 10000
� 0 <= strs[i].length <= 100
� strs[i] consists of lowercase English letters.
Hints
?? Every anagram group needs the same canonical key.
?? A sorted string can be used as a key.
?? For lowercase English letters, a 26-element frequency signature avoids sorting.
Expected Complexity
Time: O(total characters) with frequency signatures
Space: O(total characters)
Follow-up
Can you group the strings without sorting every individual string?
Practice Notes
Attempts: 0
Time spent: 0min
Hints used: 0
JavaScript
00:00
Loading...
Case 1
Case 2
Case 3
Input
["eat","tea","tan","ate","nat","bat"]Expected
[["eat","tea","ate"],["tan","nat"],["bat"]]