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

# Valid Permutations for DI Sequence

> Tested Python solution for LeetCode 903 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 903, [Hard](/catalog/hard). Topics: [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/valid-permutations-for-di-sequence/description/).

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

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

## Problem

You are given a string `s` of length `n` where `s[i]` is either:

* `'D'` means decreasing, or
* `'I'` means increasing.

A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` is called a **valid permutation** if for all valid `i`:

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

Return *the number of **valid permutations*** `perm`. Since the answer may be large, return it **modulo** `10^9 + 7`.

### Examples

```
Input: s = "DID"
Output: 5
```

**Explanation:** The 5 valid permutations of (0, 1, 2, 3) are:
(1, 0, 3, 2)
(2, 0, 3, 1)
(2, 1, 3, 0)
(3, 0, 2, 1)
(3, 1, 2, 0)

```
Input: s = "D"
Output: 1
```

### Constraints

* `n == s.length`
* `1 <= n <= 200`
* `s[i]` is either `'I'` or `'D'`.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2)
    # Space: O(n)
    def num_perms_di_sequence(self, s: str) -> int:
        mod = 1_000_000_007
        n = len(s)
        # dp[j] = ways to place values so far where the last value is the
        # j-th smallest of the values still unused.
        dp = [1] * (n + 1)
        for i, ch in enumerate(s):
            m = n + 1 - i
            ndp = [0] * (m - 1)
            if ch == "I":
                # next value is larger: its rank is at least the last rank
                run = 0
                for j in range(m - 1):
                    run = (run + dp[j]) % mod
                    ndp[j] = run
            else:
                # next value is smaller: its rank is strictly below the last rank
                suf = 0
                for j in range(m - 2, -1, -1):
                    suf = (suf + dp[j + 1]) % mod
                    ndp[j] = suf
            dp = ndp
        return dp[0]
```

## Complexity

| Time | Space |
| - | - |
| O(n^2) | O(n) |

## Tags


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