Valid Permutations for DI Sequence - Problem
Valid Permutations for DI Sequence
You're given a string
Your task is to find how many valid permutations of numbers
A permutation
For example, if
Goal: Return the count of valid permutations modulo
You're given a string
s of length n containing only two characters:'D'means decreasing - the next number should be smaller'I'means increasing - the next number should be larger
Your task is to find how many valid permutations of numbers
[0, 1, 2, ..., n] can satisfy this sequence pattern.A permutation
perm is valid if:- When
s[i] == 'D', thenperm[i] > perm[i+1] - When
s[i] == 'I', thenperm[i] < perm[i+1]
For example, if
s = "DI", we need a 3-number permutation where the first pair decreases and the second pair increases.Goal: Return the count of valid permutations modulo
109 + 7. Input & Output
example_1.py — Basic DI sequence
$
Input:
s = "DI"
›
Output:
2
💡 Note:
For sequence "DI" with numbers [0,1,2], we need first > second and second < third. Valid permutations are: [1,0,2] (1>0 ✓, 0<2 ✓) and [2,0,1] (2>0 ✓, 0<1 ✓). Total: 2 valid permutations.
example_2.py — Single character
$
Input:
s = "D"
›
Output:
1
💡 Note:
For sequence "D" with numbers [0,1], we need first > second. Valid permutations: [1,0]. Only 1 valid permutation.
example_3.py — Increasing sequence
$
Input:
s = "I"
›
Output:
1
💡 Note:
For sequence "I" with numbers [0,1], we need first < second. Valid permutations: [0,1]. Only 1 valid permutation.
Constraints
- 1 ≤ s.length ≤ 200
- s[i] is either 'D' or 'I'
- Result should be returned modulo 109 + 7
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code