Given an array of strings strs, group the anagrams together. Return the groups as a list of lists.
Use a hash map keyed by a canonical signature for each string — either the sorted characters or a tuple of 26 character counts. Strings sharing a signature land in the same bucket.
Return the groups in any order, and the strings inside each group in any order too.
1 ≤ strs.length ≤ 10⁴0 ≤ strs[i].length ≤ 100strs[i] consists of lowercase English letters.Two anagrams collapse to the same sorted string — use that as the bucket key.
Two words are anagrams when they use the same letters the same number of times. Order is the only thing that differs. So if you can turn every word into a form that forgets the order, anagrams turn into the same thing, and grouping them is just collecting equal values.
Time O(n · L)Space O(n · L)
The full walkthrough and code are part of Premium