java.lang.Object
g0901_1000.s0903_valid_permutations_for_di_sequence.Solution

public class Solution extends Object
903 - Valid Permutations for DI Sequence.<p>Hard</p> <p>You are given a string <code>s</code> of length <code>n</code> where <code>s[i]</code> is either:</p> <ul> <li><code>'D'</code> means decreasing, or</li> <li><code>'I'</code> means increasing.</li> </ul> <p>A permutation <code>perm</code> of <code>n + 1</code> integers of all the integers in the range <code>[0, n]</code> is called a <strong>valid permutation</strong> if for all valid <code>i</code>:</p> <ul> <li>If <code>s[i] == 'D'</code>, then <code>perm[i] > perm[i + 1]</code>, and</li> <li>If <code>s[i] == 'I'</code>, then <code>perm[i] < perm[i + 1]</code>.</li> </ul> <p>Return <em>the number of <strong>valid permutations</strong></em> <code>perm</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;DID&rdquo;</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> 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)</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;D&rdquo;</p> <p><strong>Output:</strong> 1</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == s.length</code></li> <li><code>1 <= n <= 200</code></li> <li><code>s[i]</code> is either <code>'I'</code> or <code>'D'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numPermsDISequence

      public int numPermsDISequence(String s)