833. Distinct Subsequence Count

Hard7.5 points estimatedStringDynamic Programming4 hints

A subsequence of a string keeps some of its characters in their original order, possibly skipping characters in between. Different choices of positions can spell the same text, for example the two x characters of xx.

Given a lowercase string s, return the number of different non-empty texts that can be obtained as subsequences of s. Because the count grows exponentially, return it modulo 1000000007. Aim for time O(n) and constant extra space.

Example 1

Input:
s = "tent"
Output:
14
Explanation:

The two t characters give identical texts for several index choices, and each text is counted once; the total number of different non-empty subsequences is 14.

Example 2

Input:
s = "nn"
Output:
2
Explanation:

Only n and nn are different texts: 2.

Example 3

Input:
s = "rotor"
Output:
26
Explanation:

Many index choices spell the same text and count once, leaving 26.

Constraints

1 ≤ s.length ≤ 105

s consists of lowercase English letters.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 600 msC++ 150 msJava 300 msJavaScript 300 msTypeScript 300 ms

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…