Leetcode - Is Subsequence
Leetcode - Ransom Note

Leetcode - Number of Matching Subsequences

violet posted @ May 02, 2020 07:47:09 AM in 算法 with tags Algorithm Golang array , 214 阅读

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 in words that are a subsequence of S: "a", "acd", "ace".

Note:

  • All words in words and S 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)
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter