Problem
FAANG · LeetCode #242

Valid Anagram — three-stage drill

1The ProblemEasy

Valid Anagram

Do two strings use exactly the same letters, the same number of times?

are these anagrams?scattactsame letters, same countsreturntrue
Examples
"cat", "act"→ true"rat", "car"→ false

constraintLowercase letters; anagram = same multiset of characters.

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 → true

Step through it

phase 1 — count s (++)scatch›tact↩ ANSWERanagram =?letter → countcount = {c: 1}
count s: 'c' → c = 1
Step 1 / 6
Arrow keys to navigate · escape to reset

All problems by pattern

Backtracking

Recursive Descent