HTMLify
remove-k-digits.py
Views: 2 | Author: prakhardoneria
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Solution: def removeKdig(self, s, k): if len(s) == k: return "0" stack = [] for digit in s: while k > 0 and stack and stack[-1] > digit: stack.pop() k -= 1 stack.append(digit) while k > 0: stack.pop() k -= 1 res = "".join(stack).lstrip('0') return res if res else "0" |