-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclear-digits_3174.py
69 lines (45 loc) · 1.56 KB
/
clear-digits_3174.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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.
# Example 1:
# Input: s = "abc"
# Output: "abc"
# Explanation:
# There is no digit in the string.
# Example 2:
# Input: s = "cb34"
# Output: ""
# Explanation:
# First, we apply the operation on s[2], and s becomes "c4".
# Then we apply the operation on s[1], and s becomes "".
# Constraints:
# 1 <= s.length <= 100
# s consists only of lowercase English letters and digits.
# The input is generated such that it is possible to delete all digits.
# ---------------------------------------Runtime 55 ms Beats 100.00% Memory 16.51 MB Beats 100.00%---------------------------------------
from collections import deque
class Solution:
def is_digit(self, item: str) -> bool:
return item.isdigit()
def clearDigits(self, s: str) -> str:
N = len(s)
if N == 1:
return s
# `i` it would be the last vowel
# `j` it would be the last digit
i = j = 0
arr = deque()
while i <= j <= N - 1:
if self.is_digit(s[i]):
i += 1
if not self.is_digit(s[j]):
arr.append(s[j])
i = j
j += 1
continue
if not self.is_digit(s[i]) and self.is_digit(s[j]):
arr.pop()
i = i >= 1 and i - 1 or 0
j += 1
return "".join(arr)