Skip to main content

One post tagged with "Stack"

View All Tags

class Solution1:
def backspaceCompare(self, s: str, t: str) -> bool:
ss, st = [], []
for c in s:
if c != '#':
ss.append(c)
elif ss:
ss.pop()
for c in t:
if c != '#':
st.append(c)
elif st:
st.pop()
return ss == st


class Solution:
def backspaceCompare(self, s: str, t: str) -> bool:
ps, pt = len(s) - 1, len(t) - 1 # pointer
bs, bt = 0, 0 # backspace

def go_left(string, pointer, backspace):
while pointer >= 0 and (backspace or string[pointer] == '#'):
backspace += (-1) ** (string[pointer] != '#')
pointer -= 1
return pointer, backspace

while True:
(ps, bs), (pt, bt) = go_left(s, ps, bs), go_left(t, pt, bt)
if not (ps >= 0 and pt >= 0 and s[ps] == t[pt]):
return ps == pt == -1
ps -= 1
pt -= 1