Mid/SeniorStackString

Remove All Adjacent Duplicates in String II

You are given a string s and an integer k. A k duplicate removal consists of choosing k adjacent and equal letters from s and removing them, causing the left and right sides of the deleted substring to concatenate together.

Repeatedly make k duplicate removals on s until no more such removals can be made.

Return the final string after all such duplicate removals have been made. It is guaranteed that the answer is unique.

Example 1
Inputs = "abcd", k = 2
Output"abcd"
There is nothing to delete because no two adjacent letters are equal.
Example 2
Inputs = "deeedbbcccbdaa", k = 3
Output"aa"
First delete "eee" and "ccc" to get "ddbbbdaa", then delete "bbb" to get "dddaa", and finally delete "ddd" to get "aa".

Constraints

  • 1 <= s.length <= 10^5
  • 2 <= k <= 10^4
  • s only contains lowercase English letters.

Asked at 18 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