AlgoFrames

Distinct subsequences

Challenge · Write code · about 25 min

Count how many distinct subsequences of s equal t. Two subsequences are different when they use different index sets, even if they spell the same string.

Example 1

Input:

["rabbbit","rabbit"]

Output:

3

Three ways to keep two of the three b characters.

Example 2

Input:

["babgbag","bag"]

Output:

5

Constraints

  • 1 ≤ s.length ≤ 1000
  • 1 ≤ t.length ≤ 100
  • The answer fits in a 32-bit integer.

Learn the idea first: String DP: LCS, edit distance, palindromic subsequence, a visual lesson.

More String DP: LCS, edit distance, palindromic subsequence problems