792 Number of Matching Subsequences

1. Question

Given stringSand a dictionary of wordswords, find the number ofwords[i]that is a subsequence ofS.

Example :

Input: S = "abcde"
words = ["a", "bb", "acd", "ace"]

Output: 3

Explanation:
There are three words in words that are a subsequence of S: "a", "acd", "ace".

Note:

  • All words inwordsandSwill only consists of lowercase letters.

  • The length ofSwill be in the range of[1, 50000].

  • The length ofwordswill be in the range of [1, 5000].

  • The length ofwords[i]will be in the range of[1, 50].

2. Implementation

(1) Brute Force

(2) HashMap + Queue

(3) Memoization

3. Time & Space Complexity

Brute Force: 时间复杂度O(nL), n为word的个数,L为S的长度, 空间复杂度O(1)

HashMap + Queue: 时间复杂度O(nL), 空间复杂度O(n)

Memoization: 时间复杂度O(nL), 空间复杂度O(n)

Last updated

Was this helpful?