You are given a string s consisting of lowercase English letters.
You must repeatedly perform the following operation while the string s has at least two consecutive characters:
'a' and 'b', or 'b' and 'a').Return the resulting string after no more operations can be performed.
Note: Consider the alphabet as circular, thus 'a' and 'z' are consecutive.
Example 1:
Input: s = "abc"
Output: "c"
Explanation:
"ab" from the string, leaving "c" as the remaining string."c".Example 2:
Input: s = "adcb"
Output: ""
Explanation:
"dc" from the string, leaving "ab" as the remaining string."ab" from the string, leaving "" as the remaining string."".Example 3:
Input: s = "zadb"
Output: "db"
Explanation:
"za" from the string, leaving "db" as the remaining string."db".
Constraints:
1 <= s.length <= 105s consists only of lowercase English letters.Problem Overview: You are given a string and repeatedly remove adjacent character pairs that satisfy the removal rule defined in the problem. After no more valid adjacent pairs exist, return the final resulting string.
Approach 1: Repeated String Simulation (Brute Force) (Time: O(n^2), Space: O(n))
The most direct method repeatedly scans the string and removes adjacent pairs whenever they satisfy the removal condition. After each removal, the string shrinks and the scan restarts because new removable pairs may appear. This simulates the process exactly as described. The downside is efficiency: each removal can trigger another full pass over the string, leading to quadratic time in the worst case. This approach is useful for understanding the mechanics of the problem but is rarely acceptable for large inputs.
Approach 2: Stack-Based Simulation (Time: O(n), Space: O(n))
A stack models the removal process efficiently. Iterate through the string once. For each character, compare it with the character at the top of the stack. If the two characters form a removable adjacent pair according to the rule, pop the stack (which simulates removing the pair). Otherwise, push the current character onto the stack. This works because any newly formed adjacency after a removal will automatically be checked with the next incoming character.
The key insight: removals only depend on the most recent unresolved character. A stack preserves exactly that state. Each character is pushed once and popped at most once, giving linear time complexity. This turns what looks like repeated rescanning into a single pass simulation.
After processing the entire string, the stack contains the remaining characters in order. Join them to produce the resulting string. The algorithm processes each character once, making it optimal for large inputs.
This problem is a classic pattern combining string processing with a stack to handle pair cancellations. The same technique appears in problems involving bracket validation, adjacent duplicate removal, and collision simulations.
Recommended for interviews: The stack-based solution is what interviewers expect. The brute force simulation demonstrates that you understand the removal process, but the stack approach shows algorithmic maturity by converting repeated rescans into a single linear pass.
We can use a stack to simulate the process of removing adjacent characters. Iterate through each character in the string. If the character at the top of the stack and the current character are consecutive (i.e., their ASCII values differ by 1 or 25), pop the top character from the stack; otherwise, push the current character onto the stack. Finally, the characters remaining in the stack are those that can no longer be removed. Join the characters in the stack into a string and return it.
The time complexity is O(n), and the space complexity is O(n), where n is the length of the string.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Repeated String Simulation (Brute Force) | O(n^2) | O(n) | Useful for understanding the removal process or small inputs |
| Stack-Based Simulation | O(n) | O(n) | Optimal approach for general cases and interview solutions |
Q2: Resulting String After Adjacent Removals | LeetCode Java Solution | Weekly Contest 451 • ExpertFunda • 249 views views
Watch 7 more video solutions →Practice Resulting String After Adjacent Removals with our built-in code editor and test cases.
Practice on FleetCode