Lexicographically Smallest Queue String

You are given a string s and an integer k.

In one move, you may choose any one character among the first k characters of s and move it to the end of the string. You can perform this move any number of times.

Return the lexicographically smallest string that can be obtained after applying these moves.

Example 1:

Input: s = "daily", k = 1

Output: "ailyd"

Explanation:

Since k = 1, in every move you can only take the first character and move it to the end.

So the only strings you can form are rotations of "daily":

  • "daily"
  • "ailyd"
  • "ilyda"
  • "lydai"
  • "ydail"

Among these, "ailyd" is the lexicographically smallest.

Example 2:

Input: s = "dcab", k = 2

Output: "abcd"

Explanation:

Because k = 2, you can choose from the first two characters in each move.

By performing a suitable sequence of moves, the characters can be rearranged to form the smallest possible string, which is "abcd".

Now Your Turn!

Pick the correct output for the given input

Input: s = "bca", k = 1

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= s.length <= 1000
  • 1 <= k <= s.length
  • s contains only lowercase English letters.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
string orderlyQueue(string s, int k) {
// Your code goes here
}
};
Test Case

Input:

K
S