/115. Distinct Subsequences

115. Distinct Subsequences

Hard
Strings51.6% acceptance

Given two strings source_str and target_str, return the number of distinct subsequences of source_str that are equal to target_str. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

Example 1

Input: source_str=aabc, target_str=abc

Output: 2

Explanation: There are 2 distinct subsequences of aabc that equal abc: aabc and abc.

Example 2

Input: source_str=xxyyzz, target_str=xyz

Output: 8

Explanation: There are 8 distinct subsequences of xxyyzz that equal xyz.

Constraints

  • 1 <= len(source_str) <= 1000
  • 1 <= len(target_str) <= 1000
  • source_str and target_str consist only of English letters
Python (current runtime)

Case 1

Input: source_str='abcd', target_str='acd'

Expected: 1

Case 2

Input: source_str='aaaa', target_str='aa'

Expected: 6

Case 3

Input: source_str='abcabc', target_str='abc'

Expected: 8

Case 4

Input: source_str='zzzz', target_str='zz'

Expected: 6

Case 5

Input: source_str='abcdef', target_str='gh'

Expected: 0