Remove All Adjacent Duplicates in String II
mediumTime: O(n)Space: O(n)
Recognize the pattern
remove adjacent groups of k identical charactersgeneralized from pairs to groups of kstack with counts
Brute force idea
Repeatedly scan and remove — O(n²/k).
Better approach
Stack of (char, count). Push chars, increment if same as top. When count = k, pop. O(n).
Key invariant
The (char, count) stack tracks consecutive identical characters. Removal at k automatically exposes new adjacencies.
Watch out for
Not tracking counts — plain char stack requires recounting after each removal.