Remove groups of adjacent equal characters repeatedly until no such group remains.
Given a string s and an integer k, repeatedly delete any block of exactly k consecutive equal characters from the string. Continue applying this rule until no removable block remains.
Your task is to return the final string after all possible removals have been performed.
Because deleting one block can create a new adjacent block across the deletion boundary, the removals must be applied repeatedly until the string becomes stable.
Input Format
- A string
sconsisting of lowercase English letters. - An integer
krepresenting the block size to remove.
Output Format
- Return the string that remains after all adjacent groups of length
khave been removed repeatedly.
Constraints
scontains only lowercase English letters- The answer is guaranteed to fit in memory
Example 1
Input
s = "deeedbbcccbdaa", k = 3
Output
"aa"
Explanation
Remove eee, then bbb, then ccc. The string becomes aa after all cascading removals are applied.
Example 2
Input
s = "pbbcggttciiippooaais", k = 2
Output
"ps"
Explanation
Repeatedly remove adjacent pairs such as bb, gg, tt, iii (after intermediate merges), pp, oo, aa, and ii until only ps remains.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.