File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ //  Time Complexity is O(N) where N is the size of the input string.
2+ //  Space complexity is O(N) as well
3+ class  Solution  {
4+ public: 
5+     string removeKdigits (string num, int  k) {
6+       int  n = num.size ();
7+       
8+       stack<char >s;
9+       int  count = k;
10+       
11+       for (int  i = 0  ; i < n; i++)
12+       {
13+         while (!s.empty () && count > 0  && s.top () > num[i])
14+         {
15+           s.pop ();
16+           count--;
17+         }
18+         s.push (num[i]);
19+       }
20+       
21+       //  In case the num was already in a non increasing order (e.x: 123456)
22+       while (s.size () != n - k) s.pop ();
23+      
24+       string res = " " 
25+       while (!s.empty ())
26+       {
27+         res += s.top ();
28+         s.pop ();
29+       }
30+       reverse (res.begin () , res.end ());
31+       //  Remove the zeros from the left if they exist.
32+       while  (res[0 ] == ' 0' erase (0  , 1 );
33+     
34+       
35+       return  (res == " " " 0" 
36+     }
37+ };
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments