Trains the technique from
LeetCode 1209Remove All Adjacent Duplicates in String IIThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A tape of tokens is written as the lowercase string s. A compactor works on it one step at a time.
A step picks any spot where k neighbouring tokens all carry the same letter, lifts exactly those k tokens off the tape, and lets the two loose ends join, which may put fresh neighbours side by side. Steps keep going while some spot still has k matching neighbours.
Whichever spots the compactor picks, the tape it finishes with is the same, so return that final tape as a string. It may be empty.
Example 1
Lifting the three `b` tokens joins the leading `a` to the three that followed them, giving `aaaac`. Three of those four `a` tokens then come off, leaving `ac`, which has no spot with three matching neighbours.
Example 2
One step removes three of the four `a` tokens. A single `a` is left and no further step applies.
Example 3
Removing the leading `pp` leaves `qqppqq`; removing that `qq` leaves `ppqq`, then `qq`, then nothing at all.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def remove_duplicates(s: str, k: int) -> str:public String removeDuplicates(String s, int k)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.