Orderly Queue

A string S of lowercase letters is given. Then, we may make any number of moves.

In each move, we choose one of the first K letters (starting from the left), remove it, and place it at the end of the string.

Return the lexicographically smallest string we could have after any number of moves.

Example 1:

Input: S = "cba", K = 1
Output: "acb"
Explanation: 
In the first move, we move the 1st character ("c") to the end, obtaining the string "bac".
In the second move, we move the 1st character ("b") to the end, obtaining the final result "acb".

Example 2:

Input: S = "baaca", K = 3
Output: "aaabc"
Explanation: 
In the first move, we move the 1st character ("b") to the end, obtaining the string "aacab".
In the second move, we move the 3rd character ("c") to the end, obtaining the final result "aaabc".

Note:

  1. 1 <= K <= S.length <= 1000

  2. S consists of lowercase letters only.

class Solution {
    public String orderlyQueue(String S, int K) {
        // If k>=2, then we can swap any 2 elements
        // Therefore -> bubble sort -> ans is always sorted string
        if (K >= 2) {
            char[] str = S.toCharArray();
            Arrays.sort(str);
            return new String(str);
        } 
        // If k==1, then we cannot swap
        // then ans is the min from n possibilities
        else {
            String min = S;
            for (int i = 1; i < S.length(); i++) {
                String str = S.substring(i) + S.substring(0, i);
                if (str.compareTo(min) < 0)
                    min = str;
            }
            return min;
        }
    }
}

Last updated