1
/**
2
* @param {string} s
3
* @param {number} k
4
* @return {string}
5
*/
6
var orderlyQueue = function (s, k) {
7
// rotate the string one by one, and check which is lexographically smaller
8
if (k === 1) {
9
let temp = `${s}`;
10
let smallest = `${s}`;
11
let count = 0;
12
while (count < s.length) {
13
temp = temp.substring(1, s.length) + temp.charAt(0);
14
if (temp < smallest) {
15
smallest = temp;
16
}
17
count++;
18
}
19
return smallest;
20
}
21

22
// if k is greater than 1, any permutation is possilbe
23
// so we simply return the sorted string (convert to array -> sort -> back to string)
24
if (k > 1) {
25
return [...s].sort().join("");
26
}
27

28
return s;
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0