スタックの連続k個の重複を除去する文字


文字列sとkが与えられ、sで連続的に繰り返されるk文字が削除できないまで削除される.例:input:「aaadbbccbbdd」,K=3 dbbbdd 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))