Facebook PixelGroup Anagrams — Coding Practice
Group AnagramsMedium

Group Anagrams

Medium 18.2k67% acceptance
StringHash TableSorting

Given an array of strings strs, group the anagrams together (anagrams share the same multiset of letters).

For a deterministic answer: sort the strings within each group in ascending order, and sort the groups lexicographically by their contents. Return that.

Example 1
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["ate","eat","tea"],["bat"],["nat","tan"]]
Constraints
  • 1 ≤ strs.length ≤ 10⁴
  • 0 ≤ strs[i].length ≤ 100
  • strs[i] is lowercase English letters.
Asked atAmazonUberMeta
JavaScript
Loading editor…
Case 1
["eat","tea","tan","ate","nat","bat"]
expected: [["ate","eat","tea"],["bat"],["nat","tan"]]
Case 2
[""]
expected: [[""]]
Case 3
["a"]
expected: [["a"]]
Case 4
["abc","cba","bac","xyz","zyx"]
expected: [["abc","bac","cba"],["xyz","zyx"]]
Case 5
["",""]
expected: [["",""]]