-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbiggerIsGreater.java
More file actions
64 lines (49 loc) · 1.76 KB
/
biggerIsGreater.java
File metadata and controls
64 lines (49 loc) · 1.76 KB
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
60
61
62
63
64
/* Given a word w, rearrange the letters of w to construct another word s in such a way that s is lexicographically
greater than w. In case of multiple possible answers, find the lexicographically smallest one among them.
Input Format
The first line of input contains t, the number of test cases. Each of the next t lines contains w.
Output Format
For each testcase, output a string lexicographically bigger than w in a separate line.
In case of multiple possible answers, print the lexicographically smallest one, and if no answer exists, print no answer. */
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static String biggerIsGreater(String s) {
int i,j,min;
String ans="";
char x;
char [] w = s.toCharArray();
for(i=w.length-1;i>0;i--)
if(w[i]>w[i-1])
break;
if(i==0)
return "no answer";
else
{
x = w[i-1];
min = i;
for(j=i+1;j<w.length;j++)
if(w[j]>x && w[j]<w[min])
min = j;
w[i-1] = w[min];
w[min] = x;
}
Arrays.sort(w,i,w.length);
for(j=0;j<w.length;j++)
ans = ans + w[j];
return ans;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for(int a0 = 0; a0 < T; a0++){
String w = in.next();
String result = biggerIsGreater(w);
System.out.println(result);
}
in.close();
}
}