Problem · String
Remove Adjacent Duplicates in String II
Learn this problemProblem statement
Given a lowercase string s and an integer k, repeatedly remove any group of exactly k adjacent equal characters. Concatenate the remaining parts after each removal.
Return the unique final string after no removable group remains.
Function
removeDuplicates(s: String, k: int) → StringExamples
Example 1
s = "deeedbbcccbdaa"k = 3return = "aa"Removing eee and ccc makes the three b characters adjacent. Removing them leaves aa.
Example 2
s = "pbbcggttciiippooaais"k = 2return = "ps"Each adjacent pair is removed as it forms, including pairs created by earlier removals.
Example 3
s = "abcd"k = 2return = "abcd"No two adjacent characters are equal.
Constraints
1 <= s.length <= 100000.scontains only lowercase English letters.2 <= k <= s.length.