Today’s problem
https://leetcode.com/problems/clear-digits/description/
Intuition
We need to pop the character before the digit in this question.
Approach
Therefore, all we need is just to utilize a stack.
Complexity
-
Time complexity: $O(N)$, N is the length of the string.
-
Space complexity: $O(N)$, N is the length of the string.
Code
class Solution:
def clearDigits(self, s: str) -> str:
st = []
for ch in s:
if '0' <= ch and ch <= '9':
st.pop()
else:
st.append(ch)
return ''.join(st)