-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
48 lines (42 loc) · 1.05 KB
/
day9.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
# def backspaceCompare(S, T):
# """
# :type S: str
# :type T: str
# :rtype: bool
# """
# #sum(i * i for i in map(int, str(n)))
# newS = []
# newT = []
# newS = [char for char in S]
# newT = [char for char in T]
# print(newS)
# print(newT)
# for i in range(0,len(newS)-1):
# if newS[i] == "#":
# del newS[i-1:i+1]
# for j in range(0,len(newT)-1):
# if newT[j] == "#":
# del newT[i-1:i+1]
# print(newS)
# print(newT)
# sp = ""
# S = sp.join(newS)
# T = sp.join(newT)
# if S == T:
# return True
#
#Input: S = "ab#c", T = "ad#c"
# Output: true
# Explanation: Both S and T become "ac".
def backspaceCompare( S: str, T: str) -> bool:
S, T = _helper(S), _helper(T)
return S == T
def _helper(s):
while "#" in s:
i = s.index("#")
s = s[:i-1] + s[i+1:] if i > 0 else s[i+1:]
return s
# for element in list:
# result += str(element)
# return result
print(backspaceCompare( "ab#c", "ad#c"))