栈之消除连续k个重复的字符

给定一个字符串s和k,删除s中连续重复的k个字符,直到不能删除为止。
示例:
input: “aaadbbcccbbdd”, K=3
dbbbbdd
dbdd
output: dbdd

def remove_k(s, k):
	stack = []
	n = len(s)
	i = 0
	while i < n:
	    temp = stack[-k+1:]
	    t = list(set(temp))
	    if len(stack) >= k-1 and len(t) == 1 and t[0] == s[i]:
	        cnt = k-1
	        while cnt:
	            stack.pop()
	            cnt -= 1
	        i += 1
	    else:
	        stack.append(s[i])
	        i += 1
	print(''.join(stack))

你可能感兴趣的:(高频算法,python,数据结构,字符串,stack)