Problem
FAANG · LeetCode #49

Group Anagrams — three-stage drill

1The ProblemMedium

Group Anagrams

Group together the strings that are anagrams of one another.

group the anagrams togethereat0tea1tan2ate3nat4bat5[eat, tea, ate] · [tan, nat] · [bat] — 3 groupsreturn3 groups
Examples
[eat,tea,tan,ate,nat,bat]→ 3 groups

constraintLowercase letters; anagrams share the same sorted letters.

2The PatternTIME O(n) · SPACE O(n)

Arrays & Hashing

Trade space for time: a hash map turns 'have I seen this?' into an O(1) lookup.

remember what you've seen in a hash map → O(1) lookup4021722344scan›value → countseen = {4: 1}
Problems using this pattern
3The SolutionRESULT → 3 groups

Step through it

wordseat0tea1tan2ate3nat4bat5↩ ANSWERgroups =—sorted key → wordsgroups = {}
group words sharing the same sorted letters
Step 1 / 8
Arrow keys to navigate · escape to reset

All problems by pattern

Backtracking

Recursive Descent