-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0792.go
61 lines (56 loc) · 1.64 KB
/
0792.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Source: https://leetcode.com/problems/number-of-matching-subsequences
// Title: Number of Matching Subsequences
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given a string s and an array of strings words, return the number of words[i] that is a subsequence of s.
//
// A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
//
// For example, "ace" is a subsequence of "abcde".
//
// Example 1:
//
// Input: s = "abcde", words = ["a","bb","acd","ace"]
// Output: 3
// Explanation: There are three strings in words that are a subsequence of s: "a", "acd", "ace".
//
// Example 2:
//
// Input: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
// Output: 2
//
// Constraints:
//
// 1 <= s.length <= 5 * 10^4
// 1 <= words.length <= 5000
// 1 <= words[i].length <= 50
// s and words[i] consist of only lowercase English letters.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
// O(n*k)
func numMatchingSubseq(s string, words []string) int {
res := 0
for _, word := range words {
res += _matchingSubseq(s, word)
}
return res
}
func _matchingSubseq(s string, word string) int {
nw := len(word)
ns := len(s)
if nw > ns {
return 0
}
i := 0
for j := 0; j < ns; j++ {
if word[i] == s[j] {
i++
if i == nw {
return 1
}
}
}
return 0
}