https://leetcode.com/problems/orderly-queue/
https://leetcode.com/problems/orderly-queue/discuss/165878/C%2B%2BJavaPython-Sort-String-or-Rotate-String
https://leetcode.com/problems/orderly-queue/discuss/165857/Java-Simple-Solution-12-ms
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".
Approach 1: Mathematical
Call the move that takes the
K
th letter from the beginning and puts it on the end, a "K
-kick" move.
Examining 1-kick moves, they let us consider the string as a "necklace" that may be rotated freely, where each bead of the necklace corresponds to a letter in the string. (Formally, this is the equivalence class under 1-kick moves.)
Examining 2-kick moves (in the context of treating the string as a necklace), they allow us to swap the positions of two adjacent beads. Thus, with 2-kick moves, every permutation of necklace is possible. (To actually construct the necklace, we bring the second smallest bead to be after the smallest, then the third smallest to be after the second smallest, and so on.)
The previous insight may be difficult to find. Another strategy is to write a brute force program to examine the result of 2-kick moves - then we might notice that 2-kick moves allow any permutation of the string.
Yet another strategy might be to explicitly construct new moves based on previous moves. If we perform a 2 kick move followed by many 1 kick moves, we can move a string like
"xyzzzzzz" -> "xzzzzzzy" -> "yxzzzzzz"
, proving we can swap the positions of any two adjacent letters.
Algorithm
If
K = 1
, only rotations of S
are possible, and the answer is the smallest rotation.
If
K > 1
, any permutation of S
is possible, and the answer is the letters of S
written in lexicographic order.- Time Complexity: , where is the length of
S
. - Space Complexity: .
public String orderlyQueue(String S, int K) {
if (K == 1) {
String ans = S;
for (int i = 0; i < S.length(); ++i) {
String T = S.substring(i) + S.substring(0, i);
if (T.compareTo(ans) < 0)
ans = T;
}
return ans;
} else {
char[] ca = S.toCharArray();
Arrays.sort(ca);
return new String(ca);
}
}
https://leetcode.com/problems/orderly-queue/discuss/165878/C%2B%2BJavaPython-Sort-String-or-Rotate-String
First, this is string rotation.
I use number instead of letters to make it clear.
12345
-> 23451
-> 34512
-> 45123
-> 51234
I use number instead of letters to make it clear.
If
There are
we return the lexicographically smallest string.
K == 1
, we can only rotate the whole string.There are
S.length
different states andwe return the lexicographically smallest string.
If
K > 1
, it means we can:- rotate the whole string,
- rotate the whole string except the first letter.
012345
->023451
->034512
->045123
->051234
We can rotate
then rotate
i+1
th big letter to the start (method 1),then rotate
i
th big letter to the end (method 2).2XXX01
-> XXX012
In this way, we can bubble sort the whole string lexicographically.
So just return sorted string.
So just return sorted string.
public String orderlyQueue(String S, int K) {
if (K > 1) {
char S2[] = S.toCharArray();
Arrays.sort(S2);
return new String(S2);
}
String res = S;
for (int i = 1; i < S.length(); i++) {
String tmp = S.substring(i) + S.substring(0, i);
if (res.compareTo(tmp) > 0) res = tmp;
}
return res;
}
Another way to generate rotate strings
which would be more efficient 12ms->8ms (Java)
which would be more efficient 12ms->8ms (Java)
String res = S;
String doubleS = S + S;
for (int i = 1; i < S.length(); i++) {
String rotate = doubleS.substring(i, i + S.length());
if (res.compareTo(rotate) > 0)
res = rotate;
}
Actually, when
K>=2
, we can prove that we can use the first 2 elements as a buffer to swap any two adjacent characters. Since we can reach any permutation by swapping adjacent characters (like bubble sort), in this case the minimal reachable permutation is the sorted S
.
Assume that we want to swap
S[i]
and S[i+1]
, we can first pop first i-1
characters to the end, then pop i+1
and i
, finally pop i+2~end
.
Example:
K=2 S=bacdb Assume that we want to swap S[1] = a and S[2] = c
K=2 S=bacdb Assume that we want to swap S[1] = a and S[2] = c
bacdb // initial
acdbb // pop first b to the end
dbbca // first pop S[2]=c, then pop S[1]=a
bbcad // pop S[3] = d
bcadb // pop S[4] = b, a and c are swapped!
https://leetcode.com/problems/orderly-queue/discuss/165857/Java-Simple-Solution-12-ms