java.lang.Object
g0901_1000.s0940_distinct_subsequences_ii.Solution

public class Solution extends Object
940 - Distinct Subsequences II.<p>Hard</p> <p>Given a string s, return <em>the number of <strong>distinct non-empty subsequences</strong> of</em> <code>s</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A <strong>subsequence</strong> of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., <code>&quot;ace&quot;</code> is a subsequence of <code>&quot;abcde&quot;</code> while <code>&quot;aec&quot;</code> is not.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;abc&rdquo;</p> <p><strong>Output:</strong> 7</p> <p><strong>Explanation:</strong> The 7 distinct subsequences are &ldquo;a&rdquo;, &ldquo;b&rdquo;, &ldquo;c&rdquo;, &ldquo;ab&rdquo;, &ldquo;ac&rdquo;, &ldquo;bc&rdquo;, and &ldquo;abc&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;aba&rdquo;</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> The 6 distinct subsequences are &ldquo;a&rdquo;, &ldquo;b&rdquo;, &ldquo;ab&rdquo;, &ldquo;aa&rdquo;, &ldquo;ba&rdquo;, and &ldquo;aba&rdquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;aaa&rdquo;</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The 3 distinct subsequences are &ldquo;a&rdquo;, &ldquo;aa&rdquo; and &ldquo;aaa&rdquo;.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 2000</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • distinctSubseqII

      public int distinctSubseqII(String str)