没看答案,用栈来实现
class Solution:
def substract(self, s: str, length: int):
if length < 1:
return s
stack = []
i = 0
res = ''
while i < length:
if s[i] != '#':
stack.append(s[i])
else:
if stack:
stack.pop()
i += 1
for ch in stack:
res += ch
return res
def backspaceCompare(self, s: str, t: str) -> bool:
res1 = self.substract(s, len(s))
res2 = self.substract(t, len(t))
return (res1 == res2)