-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackspace_string_compare.py
41 lines (29 loc) · 1.13 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
# https://leetcode.com/problems/backspace-string-compare/
class Solution(object):
def backspaceCompare(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
result_s = ""
result_t = ""
max_len = len(s) if len(s) > len(t) else len(t)
for i in range(0, max_len):
if i < len(s):
if s[i] == "#" and len(result_s) > 0:
result_s = result_s[:-1]
elif not (s[i] == "#" and len(result_s) == 0):
result_s += s[i]
if i < len(t):
if t[i] == "#" and len(result_t) > 0:
result_t = result_t[:-1]
elif not (t[i] == "#" and len(result_t) == 0):
result_t += t[i]
return result_s == result_t
solution = Solution()
print(solution.backspaceCompare("ab#c", "ad#c"))
print(solution.backspaceCompare("ab##", "c#d#"))
print(solution.backspaceCompare("a#c", "b"))
print(solution.backspaceCompare("y#fo##f", "y#f#o##f"))
print(solution.backspaceCompare("ab#c", "ad#c"))