Given a string s and an array of strings words, return the number of entries words[i] that are subsequences of s.
A subsequence of a string is a new string formed from the original by deleting some characters (possibly none) without changing the relative order of the remaining characters.
Function Signature
def num_matching_subseq(s: str, words: list[str]) -> int:
Rules
-
Each entry of
words
counts on its own. If a word appears twice in
words
and is a subsequence of
s
, it adds 2 to the answer.
-
A position of
s
can match at most one character of a word, so
"bb"
needs two
b
characters in
s
, in that order.
Constraints
-
1 <= len(s) <= 5 * 10^4
-
1 <= len(words) <= 5000
-
1 <= len(words[i]) <= 50
-
s
and every
words[i]
contain only lowercase English letters.
Examples
Input: s = "abcde", words = ["a", "bb", "acd", "ace"]
Output: 3
"a", "acd" and "ace" can be read left to right inside "abcde". "bb" cannot, because s has only one b.
Input: s = "dsahjpjauf", words = ["ahjpjau", "ja", "ahbwzgqnuk", "tnmlanowax"]
Output: 2
"ahjpjau" matches positions 2 through 8 of s, and "ja" matches positions 4 and 7. "ahbwzgqnuk" fails because s has no b, and "tnmlanowax" fails because s has no t.
Input: s = "abc", words = ["ab", "ab", "ba", "c"]
Output: 3
Both copies of "ab" count, and so does "c". "ba" fails because no a comes after the b in s.