Dashboard Temp Share Shortlinks Frames API

HTMLify

LeetCode - Clear Digits - Python
Views: 405 | Author: abh
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
    def clearDigits(self, s: str) -> str:
        ns = "01234567890"
        s = list(s[::-1])
        tl = len(s)
        for c in s:
            if c in ns:
                tl -= 2
        while len(s) > tl:
            d = None
            for i in range(len(s)):
                if s[i] in ns:
                    d = i
                    s.pop(i)
                    break
            for i in range(d, len(s)):
                if s[i] not in ns:
                    s.pop(i)
                    break
        return "".join(s[::-1])