-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackspace-string-compare.py
95 lines (84 loc) · 3.91 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
# 844. Backspace String Compare
# 🟢 Easy
#
# https://leetcode.com/problems/backspace-string-compare/
#
# Tags: Two Pointers - String - Stack - Simulation
import timeit
from itertools import zip_longest
# Use two stacks, iterate over the characters in the input strings pushing and popping as required then compare the
# resulting stacks.
#
# Time complexity: O(n) - We iterate over all elements in the input.
# Space complexity: O(n) - The stacks could grow to the same size as the input strings.
#
# Runtime: 48 ms, faster than 51.20% of Python3 online submissions for Backspace String Compare.
# Memory Usage: 13.8 MB, less than 73.78% of Python3 online submissions for Backspace String Compare.
class StackSol:
def backspaceCompare(self, s: str, t: str) -> bool:
def type(word: str) -> str:
res = []
for c in word:
if c == "#":
if res:
res.pop()
else:
res.append(c)
return res
ss = type(s)
st = type(t)
return type(s) == type(t)
# Define a function that simulates typing a word, taking into account the backspace character "#" and use it to
# obtain the result of typing both input strings, then use built-in functions to compare them.
#
# Time complexity: O(s+t) - We visit each element in the input strings twice. Once to build the "typed" string and
# once more to compare the results.
# Space complexity: O(1) - We are not using any data structures but generating values as they are used.
#
# Runtime: 33 ms, faster than 91.44% of Python3 online submissions for Backspace String Compare.
# Memory Usage: 13.8 MB, less than 98.23% of Python3 online submissions for Backspace String Compare.
class BuiltInFn:
def backspaceCompare(self, s: str, t: str) -> bool:
# Define a function that simulates typing a word. Characters input the characters and "#" simulates backspace.
def typeWord(word):
# Count the contiguous backstrokes that we have seen and haven't used yet.
skip = 0
# Iterate over the characters in the word starting from the back.
for c in reversed(word):
# If the current character is "#" add a backspace stroke to the current count.
if c == "#":
skip += 1
# If the current character is not "#" but we have unused backspace strokes, use them up "deleting"
# characters that we have typed previously. We are visiting these characters after the backspaces
# because we are traveling the word in reverse.
elif skip:
skip -= 1
# If the current character is not "#" and we don't have any unused backspaces, yield the character.
else:
yield c
# Use itertools zip with fill and all() to check if the values in both strings match. This takes advantage of
# the improved performance of the functions written in C.
return all(x == y for x, y in zip_longest(typeWord(s), typeWord(t)))
def test():
executors = [StackSol, BuiltInFn]
tests = [
["y#fo##f", "y#f#o##f", True],
["ab#c", "ad#c", True],
["ab##", "c#d#", True],
["a#c", "b", False],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(int(float("1"))):
for col, t in enumerate(tests):
sol = executor()
result = sol.backspaceCompare(t[0], t[1])
exp = t[2]
assert (
result == exp
), f"\033[93m» {result} <> {exp}\033[91m for test {col} using \033[1m{executor.__name__}"
stop = timeit.default_timer()
used = str(round(stop - start, 5))
res = "{0:20}{1:10}{2:10}".format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()