Group Anagrams

Arrays & Hashing, problem 4 of 9

Group Anagrams

Medium

LC #49

hash mapcanonical keystring

Not attempted yet

Given a list of strings strs, group together the words that are anagrams of each other (same letters, same counts). Return the groups in any order; the words inside a group can be in any order too.

Example 1

Input: strs = ["pot", "top", "tap", "opt", "pat", "cat"]
Output: [["pot", "top", "opt"], ["tap", "pat"], ["cat"]]

Example 2

Input: strs = [""]
Output: [[""]]

Example 3

Input: strs = ["a"]
Output: [["a"]]

Constraints

  • 1 ≤ len(strs) ≤ 2 · 10^4
  • 0 ≤ len(strs[i]) ≤ 100
  • Lowercase English letters only

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.