-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeast
More file actions
34 lines (33 loc) · 645 Bytes
/
Least
File metadata and controls
34 lines (33 loc) · 645 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
using namespace std;
void buildLowestNumberRec(string str, int n, string &res)
{
if (n == 0)
{
res.append(str);
return;
}
int len = str.length();
if (len <= n)
return;
int minIndex = 0;
for (int i = 1; i<=n ; i++)
if (str[i] < str[minIndex])
minIndex = i;
res.push_back(str[minIndex]);
string new_str = str.substr(minIndex+1, len-minIndex);
buildLowestNumberRec(new_str, n-minIndex, res);
}
string buildLowestNumber(string str, int n)
{
string res = "";
buildLowestNumberRec(str, n, res);
return res;
}
int main()
{
string str = "121198";
int n = 2;
cout << buildLowestNumber(str, n);
return 0;
}