#899

Orderly Queue

expert · 1080 · lc hard +32 · verified · 66.8% accepted · 1,829 likes · top 72%

Description

You have a string s and an integer k. Each operation lets you pick any character among the first k characters of s and append it to the end. Apply as many operations as you wish, then return the lexicographically smallest string achievable.

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".

Code

1
2
3