-
Notifications
You must be signed in to change notification settings - Fork 0
/
backspace-string-compare_844.py
104 lines (80 loc) · 2.54 KB
/
backspace-string-compare_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
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
# 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".
# ---------------------------------------Runtime 42 ms Beats 35.16% Memory 16.3 MB Beats 11.27%---------------------------------------
# My solution
class Solution:
def backspaceCompare(self, s: str, t: str) -> bool:
def cut_of(array: list):
stack = []
for i in range(len(array)):
val = array[i]
if val != "#":
stack.append(val)
elif val == "#":
if stack:
stack.pop()
return stack
return cut_of(s) == cut_of(t)
# Solution lee215
# Follow up: O(1) Space
from functools import reduce
class Solution:
def backspaceCompare(self, S, T):
back = lambda res, c: res[:-1] if c == "#" else res + c
return reduce(back, S, "") == reduce(back, T, "")
if __name__ == "__main__":
sol = Solution()
s = "ab#c"
t = "ad#c"
print(sol.backspaceCompare(s, t))
s = "ab##"
t = "c#d#"
print(sol.backspaceCompare(s, t))
s = "a#c"
t = "b"
print(sol.backspaceCompare(s, t))
s = "a#c"
t = "a#b"
print(sol.backspaceCompare(s, t))
s = "a##c"
t = "#a#c"
print(sol.backspaceCompare(s, t))
# class Solution:
# def backspaceCompare(self, s: str, t: str) -> bool:
# s1 = deque(s)
# t1 = deque(t)
# for s,e in zip(s, s):
# if e == '#':
# s1.popleft()
# for s,e in zip(t, t):
# if e == '#':
# t1.popleft()
# print(s1, t1)
# return s1 == t1
# class Solution:
# def backspaceCompare(self, s: str, t: str) -> bool:
# s1 = deque(s)
# t1 = deque(t)
# def cut_of(array: list):
# output = []
# start = 0
# end = len(array) - 1
# while end >= start:
# if array[end] != '#':
# output.append(array[start])
# start += 1
# end -= 1
# return output
# return cut_of(s) == cut_of(t)