algoblazerEarly Access
LearnProblemsLeaderboard
Log inSign up

All problems

‹ Back to map
1 shown · 0/261 solved

Cascading Adjacent Pair Removal

BronzeCommunity Beta
Asked atJosh Technology Group
Solve problem →
2000ms256MBAdded Aug 3, 2026Stacks & Queues

You are given a string s of lowercase English letters.

Two adjacent characters are removable if they differ by exactly 1 in the alphabet (like ab or ba), or if they are the pair a and z in either order. The alphabet wraps only between a and z, so by is not removable.

Repeatedly remove the leftmost removable adjacent pair until the string contains no removable pair. Removals cascade: deleting a pair makes its neighbors adjacent, which may create a new removable pair.

Return the result. The result may be the empty string.

Examples

Example 1

Input: s = "abcz"
Output: "cz"
Explanation: The leftmost removable pair is "ab", leaving "cz". c and z are not removable, since only a and z wrap around. Removing "bc" first would have led to a different result, which is why the leftmost rule matters.

Example 2

Input: s = "adcb"
Output: ""
Explanation: The leftmost removable pair is "dc", leaving "ab", which is itself removable. The cascade empties the string.

Example 3

Input: s = "za"
Output: ""
Explanation: z and a are the circular pair.

Constraints

  • 1≤1 \leq1≤ s.length ≤105\leq 10^5≤105
  • s consists of lowercase English letters.

Details

Solved by13 people
Time limit2000ms
Memory256MB
AddedAug 3, 20265 weeks ago
Stacks & Queues

More like this