Number of Matching Subsequences

Given a string `s` and an array of strings `words`, return the number of `words[i]` that are a subsequence of `s`. A subsequence keeps the relative order of the remaining characters after deleting some (possibly none) of them; for example, "ace" is a subsequence of "abcde".

30:00

Number of Matching Subsequences
medium
Free written solution
Topics
Companies

Given a string s and an array of strings words, return the number of words[i] that are a subsequence of s. A subsequence keeps the relative order of the remaining characters after deleting some (possibly none) of them; for example, "ace" is a subsequence of "abcde".

Example 1:
Input: {"s":"abcde","words":["a","bb","acd","ace"]}
Output: 3
Constraints:
  • 1≤s.length≤5×1041 \leq s.\text{length} \leq 5 \times 10^4

  • 1≤words.length≤50001 \leq \text{words.length} \leq 5000

  • 1≤words[i].length≤501 \leq \text{words}[i].\text{length} \leq 50

  • s and words[i] consist of only lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"abcde","words":["a","bb","acd","ace"]}

Record where each letter sits in s: a -> [0], b -> [1], c -> [2], d -> [3], e -> [4]. Each list is sorted because s is scanned left to right.

s

a

0

b

1

c

2

d

3

e

4

words, count = 0

a
bb
acd
ace
Variables
VariableValue
count0
wordnull
cur-1
DepthFunction Call
Stack empty
0/17