Advertisement
serega1112

402

Dec 23rd, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. class Solution:
  2.     def removeKdigits(self, num: str, k: int) -> str:
  3.         if len(num) == k:
  4.             return '0'
  5.         st = []
  6.        
  7.         i = 0
  8.         for d in num:
  9.             while k and st and st[-1] > d:
  10.                 st.pop()
  11.                 k -= 1
  12.             st.append(d)
  13.        
  14.         end = len(st) - k
  15.         start = 0
  16.        
  17.         while st and start < len(st) and st[start] == '0':
  18.             start += 1
  19.  
  20.         return ''.join(st[start:end]) if end > start else '0'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement