-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
899.java
59 lines (58 loc) · 2.02 KB
/
899.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
55
56
57
58
59
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public String orderlyQueue(String S, int K) {
int[] cs = new int[26];
StringBuilder sb = new StringBuilder();
for(char c: S.toCharArray()){
cs[c - 'a']++;
}
int index = -1;
for(int i = 0; i < 26 && index == -1; i++){
if(cs[i] > 0){
while(cs[i] > 0){
cs[i]--;
sb.append(Character.toString((char)('a' + i)));
if(K == 1){
index = S.indexOf((char)('a' + i));
break;
}
}
}
}
if(K == 1){
sb = new StringBuilder();
while(index != -1){
StringBuilder current = new StringBuilder();
current.append(S.charAt(index));
current.append(S.substring(index + 1, S.length()));
current.append(S.substring(0, index));
if(sb.length() == 0 || sb.toString().compareTo(current.toString()) > 0)
sb = current;
index = S.indexOf(S.charAt(index), index + 1);
}
}
return sb.toString();
}
}
__________________________________________________________________________________________________
sample 37404 kb submission
class Solution {
public String orderlyQueue(String S, int K) {
if (K >= 2) {
char[] arr = S.toCharArray();
Arrays.sort(arr);
return new String(arr);
}
String ret = S;
int len = S.length();
for (int i = 0; i < len; ++i) {
String temp = S.substring(1) + S.charAt(0);
if (temp.compareTo(ret) < 0)
ret = temp;
S = temp;
}
return ret;
}
}
__________________________________________________________________________________________________