Find All Anagrams in a String
LeetCode 438 • Medium • Sliding Window
Input: s = "cbaebabacd", p = "abc" → Output: [0, 6]
Fixed-size window (len p). Freq map of window vs p. When match, add left index.
TimeO(n)single pass
SpaceO(1)26-char maps
Window: 0
Match: —
Result: [ ]
Window
Match
Result index
Window
—
Freq
—
Result
[ ]
Ready
Press Play. Fixed-size window of len(p). When window freq matches p freq, add left to result. Slide: remove s[left], add s[right+1].