Leetcode - Number of Matching Subsequences
https://leetcode.com/problems/number-of-matching-subsequences/
Given string S
and a dictionary of words words
, find the number of words[i]
that is a subsequence of S
.
Example : Input: S = "abcde" words = ["a", "bb", "acd", "ace"] Output: 3 Explanation: There are three words inwords
that are a subsequence ofS
: "a", "acd", "ace".
Note:
-
All words in
words
andS
will only consists of lowercase letters. -
The length of
S
will be in the range of[1, 50000]
. -
The length of
words
will be in the range of[1, 5000]
. -
The length of
words[i]
will be in the range of[1, 50]
.
func numMatchingSubseq(S string, words []string) int { result := 0 for _, w := range words { if match(w, S) { result++ } } return result } func match(s, w string) bool { i := 0 j := 0 for i < len(s) && j < len(w) { if s[i] == w[j] { i++ } j++ } return i == len(s) }