class Solution:
def removeDuplicates(self, s: str, k: int) -> str:
while s:
flag = False
cur = s[0]
cnt = 1
for i in range(1, len(s)):
if cur != s[i]:
cnt = 0
cur = s[i]
cnt += 1
if cnt == k:
s = s[:i - cnt + 1] + s[i + 1:]
flag = True
break
if not flag:
break
return sclass Solution:
def removeDuplicates(self, s: str, k: int) -> str:
stack = []
n = len(s)
i = 0
s = list(s)
while i < n:
if i == 0 or s[i] != s[i - 1]:
stack.append(1)
else:
stack[-1] += 1
if stack[-1] == k:
stack.pop()
del s[i - k + 1:i + 1]
i -= k
n -= k
i += 1
return ''.join(s)class Solution:
def removeDuplicates(self, s: str, k: int) -> str:
stack = [] # [char, count]
for c in s:
if stack and stack[-1][0] == c:
stack[-1][1] += 1
else:
stack.append([c, 1])
if stack[-1][1] == k:
stack.pop()
return ''.join(char * count for char, count in stack)class Solution:
def removeDuplicates(self, s: str, k: int) -> str:
i = 0
n = len(s)
count = [0] * n
s = list(s)
for j in range(n):
s[i] = s[j]
count[i] = 1
if i > 0 and s[i - 1] == s[j]:
count[i] += count[i - 1]
if count[i] == k:
i -= k
i += 1
return ''.join(s[:i])