blob: d274575d82220ae12fece544c4effa93a1a14d02 (
plain)
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
|
# 3174. Clear Digits
"""
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.
"""
class Solution(object):
def clearDigits(self, s):
"""
:type s: str
:rtype: str
"""
stack = []
for c in s:
if c.isdigit():
if stack:
stack.pop()
else:
stack.append(c)
return "".join(stack)
if __name__ == "__main__":
obj = Solution()
print(obj.clearDigits(s="abc"))
print(obj.clearDigits(s="cb34"))
|