-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLargestNumberInKSwaps.java
36 lines (35 loc) · 1.27 KB
/
LargestNumberInKSwaps.java
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
/*https://practice.geeksforgeeks.org/problems/largest-number-in-k-swaps-1587115620/1*/
class Solution
{
static String maxValue;
public static void swapAndFind(char[] s, int k, int currSwaps)
{
if (currSwaps == k) return; //if all swaps exhausted, return
for (int j = 0; j < s.length-1; ++j) //for every digit
{
for (int i = j+1; i < s.length; ++i) //for every next digit
{
if (s[j] < s[i]) //if the picked digit is less than its next ones
{
/*backtracking step - 1*/
char temp = s[i];
s[i] = s[j];
s[j] = temp;
//update the max
maxValue = maxValue.compareTo(String.valueOf(s)) < 0 ? String.valueOf(s) : maxValue;
swapAndFind(s,k,currSwaps+1); //baxktracking step - 2
/*backtracking step - 3*/
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
public static String findMaximumNum(String str, int k)
{
maxValue = str; //store the string in max
swapAndFind(str.toCharArray(), k, 0); //recursion call
return maxValue;
}
}