Number of Matching Subsequences

Given a string s and an array of words, return how many words in the array are subsequences of s. Pattern focus: Distinct subsequences. This is a standard subsequence-counting problem often solved with buckets or DP-inspired matching states.

Input Format

s = base string, words = list of candidate words

Output Format

number of words that are subsequences of s

Constraints

  • 1 <= s.length <= 5000
  • 1 <= words.length <= 5000
  • 1 <= words[i].length <= 50
  • s and words contain lowercase English letters.

Examples

Example 1:

Input:

s = "abcde"
words = ["a","bb","acd","ace"]

Output:

3

Explanation:

The subsequences are a, acd, and ace.

Example 2:

Input:

s = "dsahjpjauf"
words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]

Output:

2

Explanation:

Two of the words can be matched as subsequences.

Loading...
Number of Matching Subsequences - Dp Strings