-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackspace-string-compare.py
49 lines (42 loc) · 1.03 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
#!/usr/bin/python3
# https://leetcode.com/problems/backspace-string-compare/
def trimString(string: str) -> str:
n = len(string)
i = 0
while i < n:
char = string[i]
if char == "#":
if i > 0:
string = string[:i - 1] + string[i + 1:]
i -= 1
n -= 2
else:
string = string[i + 1:]
n -= 1
else:
i += 1
print(string)
return string
def backspaceCompare(s: str, t: str) -> bool:
return trimString(s) == trimString(t)
if __name__ == "__main__":
testcase = (
{
"s": "#ab#c",
"t": "ad#c",
"output": True
},
{
"s": "ab##",
"t": "c#d#",
"output": True
},
{
"s": "a#c",
"t": "b",
"output": False
},
)
for test in testcase:
result = backspaceCompare(test["s"], test["t"])
print(result, result == test["output"])