Skip to content

Distinct Subsequences

HardDynamic Programming
Decode it first. Clues, constraints, pattern and plan before any code.

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.

s = "rabbbit", t = "rabbit"  →  3   (drop any one of the three b's)
s = "babgbag", t = "bag"     →  5

Constraints

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