You get a string s of lowercase letters and an integer k. One move deletes k copies of the same letter that stand next to each other in s. The text on either side of the deleted piece then closes up, which can bring more equal letters together.
Keep making moves until no k equal letters are adjacent anywhere, and return the string that remains. It may be empty. The order in which you make the moves does not change the final string.
Deleting bbb leaves aaaccb. The first a now sits next to the other two, so aaa goes as well, leaving ccb. Two cs are not enough for a move.
nn goes, giving mppm. Then pp goes, giving mm, and that pair goes too. Nothing is left.
The five zs lose two pairs and one z stays behind. The ts on either side never touch.
Constraints
1 <= s.length <= 10⁵2 <= k <= 10⁴s holds only lowercase English letters.Deleting a run can join the letters on either side into a new run, so rescanning the whole string after every deletion is too slow. Go through the string once and remember, for each letter you keep, how long its current run is.