File tree 1 file changed +49
-0
lines changed
1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Author: Aryan Yadav
3
+ Remove K Digits
4
+
5
+ Complexity: O(n)
6
+ Algorithm: NA
7
+ Difficulty: Medium
8
+ */
9
+
10
+ #include < bits/stdc++.h>
11
+ using namespace std ;
12
+ class Solution
13
+ {
14
+ public:
15
+ string removeKdigits (string num, int k)
16
+ {
17
+ string result = " " ;
18
+
19
+ minnum (num, k, result);
20
+ result.erase (0 , min (result.find_first_not_of (' 0' ), result.size () - 1 ));
21
+ if (result.length () == 0 )
22
+ result.push_back (' 0' );
23
+ return result;
24
+ }
25
+
26
+ void minnum (string num, int k, string &result)
27
+ {
28
+ if (k == 0 )
29
+ {
30
+ result.append (num);
31
+ return ;
32
+ }
33
+ int len = num.length ();
34
+ if (len <= k)
35
+ {
36
+ return ;
37
+ }
38
+
39
+ int min = 0 ;
40
+ for (int i = 1 ; i <= k; i++)
41
+ {
42
+ if (num[i] < num[min])
43
+ min = i;
44
+ }
45
+ result.push_back (num[min]);
46
+ string new_str = num.substr (min + 1 , len - min);
47
+ minnum (new_str, k - min, result);
48
+ }
49
+ };
You can’t perform that action at this time.
0 commit comments