Today’s Problem

https://leetcode.com/problems/clear-digits/description/

Solution Approach

In this problem, the characters before the number need to be removed.

Solution Method

Therefore, only a stack is required.

Complexity Analysis

  • Time complexity: $O(N)$, where N is the length of the string.

  • Space complexity: $O(N)$, where 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)