aboutsummaryrefslogtreecommitdiff
path: root/remove_k_digits.py
blob: 6355b1bd9f847c121f11c9c37f33982c1f854808 (plain)
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
# 402. Remove K Digits

"""
given string 'num' representing a non-negative integer 'num', and an integer
'k' return the smallest possible integer after removing 'k' digits from
'num'.
"""


class Solution(object):
    def removeKdigits(self, num, k):
        """
        :type num: str
        :type k: int
        :rtype: str
        """
        stk = list()
        for n in num:
            while stk and k and stk[-1] > n:
                stk.pop()
                k -= 1
            if stk or n != "0":
                stk.append(n)
        if k:
            stk = stk[0:-k]
        return "".join(stk) or "0"


if __name__ == "__main__":
    obj = Solution()
    print(obj.removeKdigits(num="1432219", k=3))
    print(obj.removeKdigits(num="10200", k=1))
    print(obj.removeKdigits(num="10", k=2))