Advertisement
Iam_Sandeep

402. Remove K Digits

Jul 6th, 2022
706
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None
  1. '''
  2. Given string num representing a non-negative integer num, and an integer k, return the smallest possible integer after removing k digits from num.
  3. '''
  4. class Solution:
  5.     def removeKdigits(self, num: str, k: int) -> str:
  6.         st=[]
  7.         if len(num)==1 and k==1:return '0'
  8.         n=len(num)
  9.  
  10.         for i in num:
  11.             while st and st[-1]>i and k>0:
  12.                 k-=1
  13.                 st.pop()
  14.             if st or i!='0':
  15.                 st.append(i)
  16.        
  17.         t=''.join(st)
  18.         if k:t=t[:-k]
  19.         return '0' if not t else t
Advertisement
RAW Paste Data Copied
Advertisement