Skip to content
Decoder

Distinct Subsequences

Hard

Problem

Given strings s and t, return the number of distinct ways to pick characters from s (keeping their order) that spell out exactly t. Two ways are different if they use a different set of positions in s.

Example 1

Input
s = "rabbbit", t = "rabbit"
Output
3
Why
drop any one of the three b's

Example 2

Input
s = "babgbag", t = "bag"
Output
5

Constraints

  • 1 <= s.length, t.length <= 1000
  • Both strings contain only English letters.
  • The answer fits in a 32-bit signed integer.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.