5. Group Anagrams

Group the words in strs so that anagrams end up together. Return the groups in any order; words inside a group can be in any order too.

Example 1

Input: strs = ["note","tone","act","cat","stone","onset","tac"]
Output: [["note","tone"],["act","cat","tac"],["stone","onset"]]

Example 2

Input: strs = ["ab","ba","abc","cab","bca","xyz"]
Output: [["ab","ba"],["abc","cab","bca"],["xyz"]]

Constraints

  • 1 ≤ strs.length ≤ 10⁴
  • 0 ≤ strs[i].length ≤ 100
Hint 1

Find a key that is identical for all anagrams: the sorted word, or its letter counts.

Hash TableStringSorting