创建栈,判断要入栈的字符与出栈的字符是否相等,若相等就真的出栈,否则就继续迭代
class Solution(object):
def removeDuplicates(self, S):
"""
:type S: str
:rtype: str
"""
stack=[]
for i in S:
if len(stack)==0:
stack.append(i)
else:
if stack[-1]==i:
stack.pop()
else:
stack.append(i)
return "".join(stack)