algorythms
Arrays & Hashing
LC #49Medium

Group Anagrams

Arrays & Hashing
AmazonMetaGoogleMicrosoftBloomberg

Problem

Given an array of strings, group the strings that are anagrams of each other and return all groups. Example: ["eat","tea","tan","ate","nat","bat"] → [["eat","tea","ate"],["tan","nat"],["bat"]] — order of groups and within each group doesn't matter.

stringhash-mapsorting

Constraints

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

Example

Inputstrs = ["eat","tea","tan","ate","nat","bat"]
Output[["bat"],["nat","tan"],["ate","eat","tea"]]
Why

eat/tea/ate share the same sorted form "aet"

Hints — reveal one at a time