1209. Remove All Adjacent Duplicates in String II
Asked at Salesforce
Difficulty
Medium
Time
O(n)
Space
O(n)
Pattern
String
Companies
1
Problem
You are given a string s and an integer k. A k-duplicate removal means picking k equal letters that sit next to each other in the string and deleting them. After the delete, the part on the left and the part on the right join together, which may create new groups of k equal letters.
Keep doing these removals until no group of k equal adjacent letters is left. Return the final string. The problem guarantees the answer is always the same, no matter which valid group you delete first.
Note that a removal only happens when exactly k equal letters are adjacent, and the letters must be next to each other in the current string — not just somewhere in the original string.
Delete k equal neighbors, then repeat
Constraints
- ●
1 <= s.length <= 10^5 - ●
2 <= k <= 10^4 - ●
s contains only lowercase English letters
Examples
s = "abcd", k = 2"abcd"s = "deeedbbcccbdaa", k = 3"aa"s = "pbbcggttciiippooaais", k = 2"ps"Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Salesforce | Medium | View all Salesforce questions → |
How to Think About It
Brute force: scan the string repeatedly and remove k adjacent duplicates until no changes occur.
Use a stack to store characters along with their consecutive counts.
When pushing a character, check if it matches the top of the stack.
If it matches, increment the count at the top; if count reaches k, pop that entry.
If it does not match, push the character with count 1 onto the stack.
Optimal Approach
Use a stack where each element stores a character and its consecutive count. Iterate through the string: if the current character equals the top of the stack, increment the count; otherwise push a new entry with count 1. Whenever a count reaches k, pop that entry. After processing the entire string, reconstruct the result by repeating each character in the stack by its count. This runs in O(n) time and O(n) space.
Stack of (letter, run length)
What Trips People Up in Real Interviews
Clarify whether removals cascade (yes, they do).
Mention that a single pass with a stack is optimal.
Discuss edge cases: entire string removed, no removals needed.
Explain why a naive string-builder approach is O(n*k) worst case.
Talk about reconstructing the result from the stack efficiently.
Solution Code
O(n) timedef removeDuplicates(s, k):
stack = []
for c in s:
if stack and stack[-1][0] == c:
stack[-1][1] += 1
if stack[-1][1] == k:
stack.pop()
else:
stack.append([c, 1])
return ''.join(c * cnt for c, cnt in stack)Frequently Asked Questions
What is the Remove All Adjacent Duplicates in String II problem?
Given a string s and an integer k, remove k adjacent identical characters repeatedly until no more removals are possible. Return the final string after all possible removals have been performed.
How do you solve Remove All Adjacent Duplicates in String II?
The optimal approach runs in O(n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Remove All Adjacent Duplicates in String II?
Remove All Adjacent Duplicates in String II is asked at Salesforce. It is a medium difficulty problem.
What are common mistakes on Remove All Adjacent Duplicates in String II?
- Clarify whether removals cascade (yes, they do).
- Mention that a single pass with a stack is optimal.
- Discuss edge cases: entire string removed, no removals needed.
- Explain why a naive string-builder approach is O(n*k) worst case.
- Talk about reconstructing the result from the stack efficiently.