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

# Find Permutation Python Solution with Tests

> Tested Python solution for LeetCode 484 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 484, [Medium](/catalog/medium). Topics: [Stack](/catalog/topics/stack), [Greedy](/catalog/topics/greedy), [Array](/catalog/topics/array), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/find-permutation/description/).

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

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

## Problem

A permutation `perm` of `n` integers of all the integers in the range `[1, n]` can be represented as a string `s` of length `n - 1` where:

* `s[i] == 'I'` if `perm[i] < perm[i + 1]`, and
* `s[i] == 'D'` if `perm[i] > perm[i + 1]`.

Given a string `s`, reconstruct the lexicographically smallest permutation `perm` and return it.

### Examples

```
Input: s = "I"
Output: [1,2]
Explanation: [1,2] is the only legal permutation that can represented by s, where the number 1 and 2 construct an increasing relationship.
```

```
Input: s = "DI"
Output: [2,1,3]
Explanation: Both [2,1,3] and [3,1,2] can be represented as "DI", but since we want to find the smallest lexicographical permutation, you should return [2,1,3].
```

### Constraints

* `1 <= s.length <= 10^5`
* `s[i]` is either `'I'` or `'D'`.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1) extra (output excluded)
    def find_permutation(self, s: str) -> list[int]:
        n = len(s) + 1
        perm = list(range(1, n + 1))
        i = 0
        while i < len(s):
            if s[i] == "D":
                j = i
                while j < len(s) and s[j] == "D":
                    j += 1
                perm[i : j + 1] = perm[i : j + 1][::-1]
                i = j
            else:
                i += 1
        return perm
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) extra (output excluded) |

## Tags

[NeetCode All](/catalog/neetcode).


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