HTMLify

LeetCode - Clear Digits - Python
Views: 13 | Author: abh
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])

Comments