> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Orderly Queue Python Solution with Tests

> Tested Python solution for LeetCode 899 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 899, [Hard](/catalog/hard). Topics: [Math](/catalog/topics/math), [String](/catalog/topics/string), [Sorting](/catalog/topics/sorting), Lexicographically Minimal String Rotation. [View on LeetCode](https://leetcode.com/problems/orderly-queue/description/).

Generate this problem as a practice environment: tested reference solution, 24 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 899   # by problem number
lcpy gen -s orderly_queue   # by problem name
```

## Problem

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

### Examples

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

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/orderly_queue/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/orderly_queue/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2) for k == 1 (n rotations of length n), O(n log n) for k >= 2
    # Space: O(n) for the rotation candidates
    def orderly_queue(self, s: str, k: int) -> str:
        if k == 1:
            return min(s[i:] + s[:i] for i in range(len(s)))
        return "".join(sorted(s))
```

## Complexity

| Time | Space |
| - | - |
| O(n^2) for k == 1 (n rotations of length n), O(n log n) for k >= 2 | O(n) for the rotation candidates |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.