-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path844-Backspace-String-Compare.py
96 lines (81 loc) · 2.05 KB
/
844-Backspace-String-Compare.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import itertools
"Leetcode- https://leetcode.com/problems/backspace-string-compare/ "
'''
Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.
Note that after backspacing an empty text, the text will continue empty.
Example 1:
Input: s = "ab#c", t = "ad#c"
Output: true
Explanation: Both s and t become "ac".
'''
# Solution-1
def backspaceCompare(self, S, T):
ansS = []
for c in S:
if c == '#':
if ansS:
ansS.pop()
else:
ansS.append(c)
ansT = []
for c in T:
if c == '#':
if ansT:
ansT.pop()
else:
ansT.append(c)
return ''.join(ansS) == ''.join(ansT)
#or#
def backspaceCompare(self, S, T):
def build(S):
ans = []
for c in S:
if c != '#':
ans.append(c)
elif ans:
ans.pop()
return "".join(ans)
return build(S) == build(T)
# T:O(M+N)
# S:O(M+N)
# Solution-2
def backspaceCompare(self, s, t):
l1 = len(s) - 1
l2 = len(t) - 1
while l1 > -1 or l2 > -1:
# count how many backspace
count = 0
while l1 > -1:
if s[l1] == "#":
count += 1
l1 -= 1
else:
if count == 0:
# not backspace, move on
break
else:
# there are backspaces, delete curr char.
l1 -= 1
count -= 1
count = 0
while l2 > -1:
if t[l2] == "#":
count += 1
l2 -= 1
else:
if count == 0:
break
else:
l2 -= 1
count -= 1
# compare two pointers
if l1 > -1 and l2 > -1 and s[l1] != t[l2]:
return False
l1 -= 1
l2 -= 1
if l1 == l2:
return True
else:
False
# T:O(M+N)
# S:O(1)