class Solution:
def orderlyQueue(self, s: str, k: int) -> str:
if k == 1:
# Only rotations are allowed, find the minimal rotation
return min(s[i:] + s[:i] for i in range(len(s)))
else:
# Any permutation is possible, so just return sorted string
return ''.join(sorted(s))
class Solution {
public:
string orderlyQueue(string s, int k) {
if (k == 1) {
string res = s;
for (int i = 1; i < s.size(); ++i) {
string rotated = s.substr(i) + s.substr(0, i);
if (rotated < res) res = rotated;
}
return res;
} else {
sort(s.begin(), s.end());
return s;
}
}
};
class Solution {
public String orderlyQueue(String s, int k) {
if (k == 1) {
String res = s;
for (int i = 1; i < s.length(); ++i) {
String rotated = s.substring(i) + s.substring(0, i);
if (rotated.compareTo(res) < 0) res = rotated;
}
return res;
} else {
char[] arr = s.toCharArray();
Arrays.sort(arr);
return new String(arr);
}
}
}
var orderlyQueue = function(s, k) {
if (k === 1) {
let res = s;
for (let i = 1; i < s.length; i++) {
let rotated = s.slice(i) + s.slice(0, i);
if (rotated < res) res = rotated;
}
return res;
} else {
return s.split('').sort().join('');
}
};
You are given a string s
and an integer k
. You can perform the following operation any number of times: choose one of the first k
letters of s
and move it to the end of the string.
Your task is to return the lexicographically smallest string that can be obtained after applying this operation any number of times.
k
letters for each move.k = 1
, you may only move the first character to the end each time (i.e., rotate the string by one).k > 1
, you may select any of the first k
letters for each move, allowing for more flexibility.
Let's break down the problem by focusing on the value of k
:
k = 1
: You can only move the first character to the end, which is equivalent to rotating the string. Therefore, the only possible strings you can obtain are all possible rotations of s
. To find the answer, you need to check all rotations and pick the smallest one lexicographically.
k > 1
: You can move any of the first k
characters to the end. With k = 2
, for example, you could move either the first or the second character. If you think further, with enough moves, you can rearrange the string in any possible order (i.e., you can generate all permutations). That means, for k > 1
, you can sort the string to get the smallest possible arrangement.
The main challenge is recognizing this difference and handling both cases efficiently.
k
k == 1
, proceed to step 2.k > 1
, proceed to step 3.k == 1
s
. For a string of length n
, there are n
rotations.
k > 1
k == 1
, the operation is limited, so brute-force through all rotations is necessary.
k > 1
, the operation is powerful enough to sort the string, so we take advantage of that for efficiency.
Example 1:
s = "cba"
, k = 1
cba
bac
acb
acb
is lexicographically smallest.acb
Example 2:
s = "baaca"
, k = 3
k > 1
, we can sort the string.a a a b c
aaabc
aaabc
k == 1
):
n
rotations for a string of length n
.O(n)
time to generate and compare.O(n^2)
O(n)
for storing rotations.k > 1
):
O(n \log n)
time.O(n)
for the sorted string.
The optimized approach is much faster for k > 1
, while k == 1
is still efficient for reasonable string lengths.
The key insight for solving the "Orderly Queue" problem is recognizing how the value of k
changes the set of possible strings you can generate. For k == 1
, you are limited to rotations, so you must brute-force all possibilities. For k > 1
, you can achieve any permutation, so simply sorting gives the answer. This dichotomy leads to a concise, elegant, and efficient solution that leverages both brute-force and optimal strategies depending on the input.