-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path844.py
45 lines (40 loc) · 1010 Bytes
/
844.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
# Backspace String Compare
def backspace(S):
S = list(S)
i = 0
while i < len(S):
if S[i] == "#":
if i-1 >= 0:
S.pop(i)
S.pop(i-1)
i -= 1
else:
S.pop(i)
else:
i += 1
return "".join(S)
class Solution:
def backspaceCompare(self, S: str, T: str) -> bool:
S = backspace(S)
T = backspace(T)
if S == T:
return True
return False
# can use a stack instead to optimise O(n)
class Solution:
def backspaceCompare(self, S: str, T: str) -> bool:
stackS = []
stackT = []
for i in S:
if i != "#":
stackS.append(i)
else:
if stackS:
stackS.pop()
for i in T:
if i != "#":
stackT.append(i)
else:
if stackT:
stackT.pop()
return stackS==stackT