Distinct Subsequences II
Hard · String, Dynamic Programming
Given a string s, return the number of distinct non-empty subsequences of s. Since the answer may be very large, return it modulo 109 + 7.
Example 1
Input: s = "abc" Output: 7
The 7 distinct subsequences are "a", "b", "c", "ab", "ac", "bc", and "abc".
Example 2
Input: s = "aba" Output: 6
The 6 distinct subsequences are "a", "b", "ab", "aa", "ba", and "aba".
Example 3
Input: s = "aaa" Output: 3
The 3 distinct subsequences are "a", "aa" and "aaa".
Constraints
- 1 <= s.length <= 2000
- s consists of lowercase English letters.
Starter code (python)
class Solution(object):
def distinctSubseqII(self, s):
"""
:type s: str
:rtype: int
"""
Related problems
- Longest Substring Without Repeating Characters (Medium)
- Longest Palindromic Substring (Medium)
- Roman to Integer (Easy)
Previous: Minimum Area Rectangle · Next: Valid Mountain Array · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.