Leetcode 每日一题
题目链接:844. 比较含退格的字符串
解题思路:用栈来解决,将两个字符串S,T处理后比较,注意栈为空时不能pop。
题解:
class Solution:
def backspaceCompare(self, S: str, T: str) -> bool:
s1 = self.solve(S)
t1 = self.solve(T)
return s1 == t1
def solve(self, string: str):
stack = []
for example in string:
if example == '#':
if len(stack) > 0:
stack.pop()
else:
stack.append(example)
return stack