给一个字符串,要求如果相邻字母相同就同时去掉两个字母,输出最后剩下的字符串,简单栈操作
1 #1047
2 #Runtime: 53 ms
3 #Memory Usage: 14.5 MB
4
5 class Solution(object):
6 def removeDuplicates(self, s):
7 """
8 :type s: str
9 :rtype: str
10 """
11 ans = []
12 for i in s:
13 if ans and i == ans[-1]:
14 ans.pop()
15 else:
16 ans.append(i)
17 return ''.join(ans)