-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackspace.py
56 lines (42 loc) · 1.23 KB
/
backspace.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
"""
Assume "#" is like a backspace in string. This means that string "a#bc#d" actually is "bd"
Your task is to process a string with "#" symbols.
Examples
"abc#d##c" ==> "ac"
"abc##d######" ==> ""
"#######" ==> ""
"" ==> ""
"#abc#d#efgh##" ==> "abef"
"""
s1 = "abc#d##c"
s2 = "abc##d######"
s3 = "#######"
s4 = "#abc#d#efgh##"
# ==== Solution 1 - Using Stack ====
def backspace(s):
stack = []
for i in range(len(s)):
if s[i] != "#":
stack.append(s[i])
else:
if stack:
stack.pop()
return "".join(stack)
assert backspace("abc#d##c") == "ac", "Expected output: 'ac'"
assert backspace("#abc#d#efgh##") == "abef", "Expected output: 'ac'"
assert backspace("#a######") == "", "Expected output: '' "
# ==== Solution 2 - Looping backward ====
def backspace2(s):
pound_count = 0
ans = ''
for i in range(len(s)-1, -1, -1):
print(i)
if s[i] != "#":
if pound_count == 0:
ans = s[i] + ans[0:]
elif pound_count > 0:
pound_count -= 1
elif s[i] == "#":
pound_count += 1
return ans
assert backspace2("abc#d##c") == "ac", "Expected output: 'ac'"