-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackspaceStringCompare.py
45 lines (39 loc) · 1.08 KB
/
BackspaceStringCompare.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution:
def backspaceCompare(self, S: str, T: str) -> bool:
i = len(S)-1
j = len(T)-1
s = ''
t = ''
s_backspace = 0
t_backspace = 0
while i >= 0 or j >= 0:
while i >= 0:
if S[i] == '#':
s_backspace += 1
i -= 1
elif s_backspace > 0:
s_backspace -= 1
i -= 1
else:
s = S[i]
break
while j >= 0:
if T[j] == '#':
t_backspace += 1
j -= 1
elif t_backspace > 0:
t_backspace -= 1
j -= 1
else:
t = T[j]
break
if i < 0:
s = ''
if j < 0:
t = ''
if s == t:
i -= 1
j -= 1
else:
return False
return True