Skip to content

Commit 5d7335e

Browse files
authored
Update clear-digits.py
1 parent 87bc7fc commit 5d7335e

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

Python/clear-digits.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
# Time: O(n)
22
# Space: O(1)
33

4-
# stack
4+
# two pointers
55
class Solution(object):
6+
def clearDigits(self, s):
7+
"""
8+
:type s: str
9+
:rtype: str
10+
"""
11+
s = list(s)
12+
j = 0
13+
for i, x in enumerate(s):
14+
if x.isdigit():
15+
j -= 1
16+
continue
17+
s[j] = x
18+
j += 1
19+
while len(s) > j:
20+
s.pop()
21+
return "".join(s)
22+
23+
24+
# Time: O(n)
25+
# Space: O(1)
26+
# stack
27+
class Solution2(object):
628
def clearDigits(self, s):
729
"""
830
:type s: str

0 commit comments

Comments
 (0)