Distinct Subsequences II

HardDynamic Programming1d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given a string s, count how many **distinct non-empty** subsequences it has. A subsequence is formed by deleting zero or more characters without changing the order of the rest; two subsequences are the same if they spell the same string, no matter which positions they came from. Return the count modulo 10^9 + 7. Aim for O(n * 26) time or better.

Examples

Input: s = "abc"
Output: 7
a, b, c, ab, ac, bc, abc.
Input: s = "aba"
Output: 6
a, b, ab, aa, ba, aba.
Input: s = "aaa"
Output: 3
a, aa, aaa.

Constraints

  • 1 <= s.length <= 2000
  • s consists of lowercase English letters.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 1d dp problems