-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathremove-all-adjacent-duplicates-in-string.py
58 lines (51 loc) · 1.72 KB
/
remove-all-adjacent-duplicates-in-string.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
# 1047. Remove All Adjacent Duplicates In String
# 🟢 Easy
#
# https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
#
# Tags: String - Stack
import timeit
# Iterate over the input string's characters pushing them into a stack,
# before pushing them, we compare the current character with the top of
# the stack, if they are the same, we pop the top of the stack and not
# push the current character.
#
# Time complexity: O(n) - We visit each character once to assess it and
# a maximum of 2 times to push it into, and pop it from the stack.
# Space complexity: O(n) - The stack could grow to the same size as the
# input.
#
# Runtime: 70 ms, faster than 97.15%
# Memory Usage: 14.7 MB, less than 86.70%
class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for c in s:
if stack and stack[-1] == c:
stack.pop()
else:
stack.append(c)
return "".join(stack)
def test():
executors = [Solution]
tests = [
["abbaca", "ca"],
["azxxzy", "ay"],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.removeDuplicates(t[0])
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()