## Algorithm

Problem Name: 899. Orderly Queue

You are given a string `s` and an integer `k`. You can choose one of the first `k` letters of `s` and append it at the end of the string..

Return the lexicographically smallest string you could have after applying the mentioned step 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".
```

Constraints:

• `1 <= k <= s.length <= 1000`
• `s` consist of lowercase English letters.

## Code Examples

### #1 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const orderlyQueue = function (S, K) {
if (K === 0) return S
else if (K > 1) return S.split('').sort().join('')
let result = 0,
L = S.length
for (let i = 1; i  <  L; i++) {
for (let j = 0; j  <  L; j++) {
let d = S.charCodeAt((result + j) % L) - S.charCodeAt((i + j) % L)
if (d !== 0) {
if (d > 0) result = i
break
}
}
}
return S.slice(result) + S.slice(0, result)
}
``````
Copy The Code &

Input

cmd
s = "cba", k = 1

Output

cmd
"acb"

### #2 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def orderlyQueue(self, S, K):
return "".join(sorted(S)) if K > 1 else min(S[i:] + S[:i] for i in range(len(S)))
``````
Copy The Code &

Input

cmd
s = "cba", k = 1

Output

cmd
"acb"