Advertisement
kosievdmerwe

Untitled

Feb 18th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. class Solution:
  2.     def removeKdigits(self, num: str, k: int) -> str:
  3.         digits = []
  4.         for d in num:
  5.             if not digits and d == "0":
  6.                 continue
  7.            
  8.             if d == "0" and k >= len(digits):
  9.                 k -= len(digits)
  10.                 digits = []
  11.                 continue
  12.            
  13.             while digits and k > 0 and d < digits[-1]:
  14.                 digits.pop()
  15.                 k -= 1
  16.             digits.append(d)
  17.        
  18.         while k > 0 and digits:
  19.             k -= 1
  20.             digits.pop()
  21.            
  22.         if not digits:
  23.             return "0"
  24.         return "".join(digits)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement