Group Anagrams

Medium#49counting

You get a list of words. Put them into groups so that each group holds the words made of the same letters, only in a different order. Our example: [arc, stop, car, pots, rat, tops] — three groups: [arc, car], [stop, pots, tops] and [rat].

input
strs: an array of strings.
returns
The strings grouped so that each group holds all the anagrams of one another: the same letters, each as many times. Groups in any order, strings in a group too.
constraints
  • 1 ≤ strs.length ≤ 1000
  • 0 ≤ strs[i].length ≤ 100
  • strs[i]: lowercase English letters
our 11 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {string[]} strs
 * @return {string[][]}
 */
function groupAnagrams(strs) {

}
Ctrl ↵
format
reset

saved in this browser

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more hash map problems

Pattern “Hash map” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.