-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_editor.py
38 lines (29 loc) · 925 Bytes
/
text_editor.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
'''
Given a string s representing characters typed into an editor, with "<-" representing a backspace, return the current state of the editor.
Constraints
n ≤ 100,000 where n is the length of s
'''
class Solution:
def solve(self, s):
i = 0
stack = []
while i < len(s):
if s[i] == '<' and i < len(s)-1 and s[i+1] == '-':
if len(stack) != 0:
stack.pop()
i += 2
else:
stack.append(s[i])
i += 1
stack = ''.join(stack)
print(stack)
return stack
----------------------------------------------------------------
class Solution:
def solve(self, s):
text = s.split("<-")
ans = text[0]
for i in range(1, len(text)):
ans = ans[:-1] # removing last character
ans += text[i]
return ans