Asked at

Group Anagrams

Medium
Verified
Hash MapStringSorting~20 min

Given an array of strings strs, group the anagrams together. Return the groups in any order.

Two strings are anagrams when they share the same letter multiset. Bucket each word by its sorted letters.

The input arrives as a single array strs.

Examples

in["eat", "tea", "tan", "ate", "nat", "bat"]
out[["eat","tea","ate"],["tan","nat"],["bat"]]

Words sharing the same letter multiset land in one group.

in["", ""]
out[["",""]]

Empty strings are anagrams of each other.

Constraints

  • 1 ≤ strs.length ≤ 10⁴
  • 0 ≤ strs[i].length ≤ 100
  • strs[i] consists of lowercase English letters.

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.