Zepruv › Coding problems

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

Starter code (python)

class Solution(object):
    def distinctSubseqII(self, s):
        """
        :type s: str
        :rtype: int
        """
        

Related problems

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.