Suppose we have a string s and another value k, we repeatedly delete the earliest k consecutive duplicate characters, and return the final string.
So, if the input is like s = "paaappmmmma" k = 3, then the output will be "ma", as when we delete three "a"s to get "pppmmmma". Then we delete three "p"s to get "mmmma". Then delete three of the four "m"s to get "ma".
To solve this, we will follow these steps:
Let us see the following implementation to get better understanding:
class Solution: def solve(self, s, k): while True: count = 0 chars = set(s) for c in chars: if c * k in s: s = s.replace(c * k, "") count += 1 if count == 0: break return s ob = Solution() s = "paaappmmmma" k = 3 print(ob.solve(s, k))