-
Notifications
You must be signed in to change notification settings - Fork 43
/
backspace-string-compare.py
126 lines (103 loc) · 3.15 KB
/
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
844. Backspace String Compare
Easy
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".
Example 2:
Input: s = "ab##", t = "c#d#"
Output: true
Explanation: Both s and t become "".
Example 3:
Input: s = "a#c", t = "b"
Output: false
Explanation: s becomes "c" while t becomes "b".
Constraints:
1 <= s.length, t.length <= 200
s and t only contain lowercase letters and '#' characters.
Follow up: Can you solve it in O(n) time and O(1) space?
"""
# V0
class Solution(object):
def backspaceCompare(self, S, T):
def check(x):
ans = []
for i in x:
if i != "#":
ans.append(i)
# both `if ans:` and `if len(ans) > 0` work
#if ans:
if len(ans) > 0:
if ans[-1] != "#" and i == "#":
ans.pop()
return ans
return check(S) == check(T)
# V1
# http://bookshadow.com/weblog/2018/06/03/leetcode-backspace-string-compare/
class Solution(object):
def backspaceCompare(self, S, T):
"""
:type S: str
:type T: str
:rtype: bool
"""
def toString(S):
ans = []
for c in S:
if c == '#': ans and ans.pop()
else: ans.append(c)
return ''.join(ans)
return toString(S) == toString(T)
# V1'
# http://bookshadow.com/weblog/2018/06/03/leetcode-backspace-string-compare/
class Solution(object):
def backspaceCompare(self, S, T):
def toString(S):
ans = []
#print ('S = ', S)
for c in S:
#print ('c = ', c )
#print ('ans = ', ans )
if c == '#':
ans and ans.pop()
else:
ans.append(c)
#print ('ans = ', ans )
return ''.join(ans)
## re-run the runc compare m treated S, T
return toString(S) == toString(T)
# V1''
# https://www.jiuzhang.com/solution/backspace-string-compare/#tag-highlight-lang-python
class Solution(object):
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)
# V2
import itertools
class Solution(object):
def backspaceCompare(self, S, T):
"""
:type S: str
:type T: str
:rtype: bool
"""
def findNextChar(S):
skip = 0
for i in reversed(range(len(S))):
if S[i] == '#':
skip += 1
elif skip:
skip -= 1
else:
yield S[i]
return all(x == y for x, y in itertools.zip_longest(findNextChar(S), findNextChar(T)))