-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bigger is Greater.cs
58 lines (57 loc) · 1.94 KB
/
Bigger is Greater.cs
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
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int t = int.Parse(Console.ReadLine());
while(t-- > 0){
char[] chr = Console.ReadLine().Trim().ToCharArray();
int len = chr.Length;
bool poss = false;
int[] pos = new int[26];
int idx = -1;
int w = -1;
bool[] small = new bool[26];
small[(int)chr[len-1] - (int)'a'] = true;
pos[(int)chr[len-1] - (int)'a'] = len - 1;
for(int i = len - 2 ; i >= 0 ; i--){
int acode = (int)chr[i] - (int)'a';
for(int j = acode + 1; j < 26 ; j++){
if(small[j])
{
poss = true;
idx = pos[j];
break;
}
}
if(poss){
w = i ;
break;
}
small[acode] = true;
pos[acode] = i;
}
//Console.WriteLine(w + "-" + idx);
if(poss){
char c = chr[w];
chr[w] = chr[idx];
chr[idx] = c;
char[] sortarr = new char[len - w - 1];
int counter = 0 ;
for(int i = w + 1 ; i < len ; i++){
sortarr[counter] = chr[i];
counter++;
}
Array.Sort(sortarr);
for(int i = 0 ; i < sortarr.Length ; i++){
chr[w + 1] = sortarr[i];
w++;
}
Console.WriteLine(new string(chr));
} else {
Console.WriteLine("no answer");
}
}
}
}