Resulting String After Adjacent Removals - Problem
You are given a string s consisting of lowercase English letters. Your task is to repeatedly remove consecutive alphabet pairs until no more removals are possible.
Rules:
- Find the leftmost pair of adjacent characters that are consecutive in the alphabet
- Characters can be in either order (e.g., 'ab' or 'ba')
- The alphabet is circular: 'a' and 'z' are consecutive
- After removal, shift remaining characters left to fill the gap
- Repeat until no more pairs can be removed
Example: "abdc" → Remove 'ab' → "dc" → Remove 'dc' → ""
Return the final string after all possible removals.
Input & Output
example_1.py — Basic removal
$
Input:
s = "abdc"
›
Output:
""
💡 Note:
Remove 'ab' (consecutive) → 'dc'. Remove 'dc' (consecutive) → empty string.
example_2.py — Circular alphabet
$
Input:
s = "zaba"
›
Output:
""
💡 Note:
Remove 'za' (circular consecutive) → 'ba'. Remove 'ba' (consecutive) → empty string.
example_3.py — No removals possible
$
Input:
s = "aceg"
›
Output:
"aceg"
💡 Note:
No adjacent characters are consecutive in alphabet, so no removals possible.
Constraints
- 1 ≤ s.length ≤ 105
- s consists of lowercase English letters only
- Alphabet is circular: 'a' and 'z' are consecutive
Visualization
Tap to expand
Understanding the Visualization
1
Initialize
Start with empty stack and process string left to right
2
Push or Pop
For each character, either cancel with top (if consecutive) or add to stack
3
Cascade Effect
Removals can create new consecutive pairs automatically
4
Final Result
Remaining stack content is the answer
Key Takeaway
🎯 Key Insight: Stack naturally handles the cascade effect where removing one pair can create new consecutive pairs that also need to be removed.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code