forked from jinghao-jia/Hackerrank_Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBiggerIsGreater.java
54 lines (46 loc) · 1.31 KB
/
BiggerIsGreater.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
48
49
50
51
52
53
54
/* Always look for the increasing string starts from the end
Then switch the char right before the string with the minimum char
that is greater than it in the increasing string. Finally, sort the
modified increasing string.
*/
import java.io.*;
import java.util.*;
public class Solution {
private static String noSol = "no answer";
public static void main(String[] args) {
boolean possible = false;
//Scanner to read input;
Scanner input = new Scanner(System.in);
//Number of queries
long strNum = input.nextInt();
long num = 0;
while(num++ < strNum) {
possible = false;
char charArr[] = input.next().toCharArray();
int count = 1;
char pre = charArr[charArr.length - 1];
int i;
for(i = charArr.length - 2; i >= 0; i--) {
if(charArr[i] >= pre)
count++;
else
break;
pre = charArr[i];
}
if(count == charArr.length) {
System.out.println(noSol);
continue;
}
for(int j = charArr.length - 1; j >= 0; j--) {
if(charArr[j] > charArr[i]) {
char temp = charArr[j];
charArr[j] = charArr[i];
charArr[i] = temp;
Arrays.sort(charArr, i + 1, charArr.length);
break;
}
}
System.out.println(new String(charArr));
}
}
}