forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
29 lines (29 loc) · 878 Bytes
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
def backspaceCompare(self, s: str, t: str) -> bool:
i, j, skip1, skip2 = len(s) - 1, len(t) - 1, 0, 0
while i >= 0 or j >= 0:
while i >= 0:
if s[i] == '#':
skip1 += 1
i -= 1
elif skip1:
skip1 -= 1
i -= 1
else:
break
while j >= 0:
if t[j] == '#':
skip2 += 1
j -= 1
elif skip2:
skip2 -= 1
j -= 1
else:
break
if i >= 0 and j >= 0:
if s[i] != t[j]:
return False
elif i >= 0 or j >= 0:
return False
i, j = i - 1, j - 1
return True