-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClear Digits.py
36 lines (27 loc) · 983 Bytes
/
Clear Digits.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
You are given a string s.
Your task is to remove all digits by doing this operation repeatedly:
Delete the first digit and the closest non-digit character to its left.
Return the resulting string after removing all digits.
-------------------------------------------------------
my solution:
class Solution:
def clearDigits(self, s: str) -> str:
stack = []
for i in range(len(s)):
if not s[i].isdigit():
stack.append(s[i])
elif s[i].isdigit and not stack[-1].isdigit():
stack.pop()
stack = ''.join(stack)
return stack
----------------------------------------------------------------------------------------
class Solution:
def clearDigits(self, s: str) -> str:
stack = []
for c in s:
if c.isdigit():
if stack:
stack.pop()
else:
stack.append(c)
return "".join(stack)