strs, collect strings that are anagrams of each other into a single group and return all groups. The order of groups, and the order inside each group, does not matter.Group Anagrams
L7 Medium Arrays & Hashing
Concept
The same sorted-letter key that identifies anagrams gives you the dictionary key to collect them into groups.
Given an array of strings
Examples
▸ strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
→ [["bat"], ["nat", "tan"], ["ate", "eat", "tea"]]
▸ strs = [""]
→ [[""]]
▸ strs = ["a"]
→ [["a"]]
Progressive Hints
Hint 1 · Nudge
Anagrams share the same sorted letters, so sorted letters make a perfect grouping key.
Hint 2 · Plan
For each word, sort its letters to build a key. Keep a dictionary mapping each key to a growing list, then return all the lists.
Hint 3 · Approach
groups = map from sorted-letters key to list. For each word: key = the word's letters sorted; append the word to groups[key]. Return the values of the dictionary.
All hints are out. Take a breath and give it a shot.
Output
// Run your code to see the output here.