-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallestLex.java
More file actions
109 lines (58 loc) · 1.78 KB
/
Copy pathSmallestLex.java
File metadata and controls
109 lines (58 loc) · 1.78 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package Prep;
import java.util.Arrays;
import java.util.Scanner;
public class SmallestLex {
public static String helper2(int k, String str) {
String smallest = str;
for (int i = 0; i < str.length(); i++) {
String temp = str.substring(i) + str.substring(0, i);
if (temp.compareTo(smallest) < 0) {
smallest = temp;
}
}
return smallest;
}
public static String helper(int k, String str) {
String ans = "";
while (str.length() != 0) {
char temp = str.charAt(0);
for (int i = 1; i < k && i < str.length(); i++) {
if (str.charAt(i) < temp) {
temp = str.charAt(i);
}
}
ans += temp;
for (int i = 0; i < k; i++) {
if (str.charAt(i) == temp) {
str = str.substring(0, i) + str.substring(i + 1);
break;
}
}
}
return ans;
}
// public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
// int t = sc.nextInt();
// while(t-->0){
// int k=sc.nextInt();
// String str = sc.next();
// System.out.println(helper(k,str));
// }
// }
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int k = sc.nextInt();
String str = sc.next();
if (k == 1) {
System.out.println(helper2(k, str));
} else {
char arr[] = str.toCharArray();
Arrays.sort(arr);
System.out.println(arr);
}
}
}
}