-
Notifications
You must be signed in to change notification settings - Fork 692
/
NextHighestPermutation.java
47 lines (36 loc) · 1005 Bytes
/
NextHighestPermutation.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
37
38
39
40
41
42
43
44
45
46
47
package strings;
public class NextHighestPermutation {
/*
* Find next highest permutation of a given string.
* Input: "abbc"
* Output: "abcb"
* Follow same logic as finding the next Highest integer from a given integer
* */
public static String permutateString(String s) {
char[] c = s.toCharArray();
int a = 0;
int n = c.length;
for(int i=n-1; i>0; i--) {
if(c[i] > c[i-1]) {
a = i;
break;
}
}
if(a == 0) return "No permutations";
int num = c[a-1];
int min = a;
for(int i=a+1; i<n; i++) {
if(c[i] > num && c[i] < c[min]) {
min = i;
}
}
char temp = c[min];
c[min] = c[a-1];
c[a-1] = temp;
return new String(c);
}
public static void main(String[] args) {
String input = "abbc";
System.out.println(permutateString(input));
}
}