-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlargestnum_k_swaps.cpp
59 lines (49 loc) · 1.18 KB
/
largestnum_k_swaps.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void findMaxUtil(string str, string &max, int k, int pos)
{
if(k == 0)
return;
char maxm = str[pos];
for(int i = pos+1; i < str.length() ;i++)
{
if(maxm < str[i])
maxm = str[i];
}
if(maxm != str[pos])
k--;
for(int i=str.length()-1; i>=pos ;i--)
{
if(str[i] == maxm)
{
swap(str[i], str[pos]);
if(str.compare(max) > 0)
max = str;
findMaxUtil(str, max, k, pos+1);
swap(str[i], str[pos]);
}
}
}
string findMaximumNum(string str, int k)
{
string max = str;
findMaxUtil(str, max, k, 0);
return max;
}
};
int main()
{
int t, k;
string str;
cin >> t;
while (t--)
{
cin >> k >> str;
Solution ob;
cout<< ob.findMaximumNum(str, k) << endl;
}
return 0;
}