Remove Adjacent Duplicates in Groups of K
Whenever k equal letters sit next to each other, delete all k of them and let the left and right parts join up. Keep doing this until no group of k equal neighbours is left, and return the string. The result does not depend on the order of deletions.
Example 1
s "deeedbbcccbdaa"
k 3
returns "aa"
Remove eee, then ccc, then bbb, then ddd: left with aa.
Constraints
1 <= len(s) <= 1002 <= k <= 10- Lowercase letters only
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Stacks and queues problems
- Remove K DigitsMedium
- Rotating Queue With CooldownMedium
- Round Robin Completion TimesMedium
- Score of ParenthesesMedium
- Self Time per FunctionMedium
- Simplify PathMedium
- Two Teams in a CircleMedium
- Visible to the RightMedium