Resulting String After Adjacent Removals

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:

  • Remove the leftmost pair of adjacent characters in the string that are consecutive in the alphabet, in either order (for example, 'a' and 'b', or 'b' and 'a').
  • Shift the remaining characters to the left to fill the gap.

Return the resulting string after no more operations can be performed.

Note: Consider the alphabet as circular, so 'a' and 'z' are consecutive.

Example 1
Inputs = "abc"
Output"c"
Remove "ab" from the string, leaving "c"; no further operations are possible.
Example 2
Inputs = "adcb"
Output""
Remove "dc" to leave "ab", then remove "ab" to leave the empty string.

Constraints

  • 1 <= s.length <= 10^5
  • s consists only of lowercase English letters.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate