g0801_0900.s0899_orderly_queue.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0801_0900.s0899_orderly_queue;
// #Hard #String #Math #Sorting #2022_03_28_Time_1_ms_(100.00%)_Space_40.5_MB_(98.64%)
import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
public String orderlyQueue(String s, int k) {
if (k > 1) {
char[] ans = s.toCharArray();
Arrays.sort(ans);
return String.valueOf(ans);
}
char min = 'z';
ArrayList list = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
char cc = s.charAt(i);
if (cc < min) {
min = cc;
}
}
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == min) {
list.add(i);
}
}
String ans = s;
for (Integer integer : list) {
String after = s.substring(0, integer);
String before = s.substring(integer);
String f = before + after;
if (f.compareTo(ans) < 0) {
ans = f;
}
}
return ans;
}
}