The Remove All Adjacent Duplicates in String II problem removes groups of k consecutive identical characters repeatedly until no more groups of k exist. This medium coding problem uses a stack storing (character, count) pairs to efficiently track runs. The string and stack interview pattern is demonstrated at an intermediate level.
Goldman Sachs, Microsoft, Meta, Amazon, TikTok, Google, Bloomberg, and others ask this as the harder variant of adjacent duplicates removal. The (character, count) stack pair elegantly handles runs of any length and triggers removal when count reaches k.
Stack of (char, count) pairs. For each character: if stack is non-empty and stack top character matches current, increment count. If count reaches k, pop the pair (group removed). Else push new pair (char, 1).
s="deeedbbcccbdaa", k=3. Process:
String II extends String I by tracking run lengths. The (char, count) stack is the cleanest approach: each push either extends a run or starts a new one; when count reaches k, pop and remove. This automatically handles cascading removals. Practice: "decode string," "compress string with counts," "run-length decoding." The (char, count) stack is a versatile string compression tool.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Remove to Make Valid Parentheses | Medium | Solve | |
| Reverse Substrings Between Each Pair of Parentheses | Medium | Solve | |
| Score of Parentheses | Medium | Solve | |
| Simplify Path | Medium | Solve | |
| Maximum Nesting Depth of Two Valid Parentheses Strings | Medium | Solve |