Remove All Adjacent Duplicates in String II
Problem statement
Given a lowercase string s and an integer k, repeatedly remove a block of exactly k adjacent equal letters. The parts on either side join after each removal and may create another removable block.
Return the final string when no removal is possible. The result is unique. For this exercise, an empty input or a completely removed string returns the empty string.
Function
removeDuplicates(s: String, k: int) → StringExamples
Example 1
s = "abbbaa"k = 3return = Removing bbb joins the surrounding letters into aaa. Removing aaa then leaves the empty string.
Example 2
s = "aabcccbd"k = 3return = aabbdRemoving ccc leaves aabbd. Every remaining run has length below three, so aabbd is final.
Constraints
0 <= s.length <= 40002 <= k <= 100scontains only lowercase English letters.