-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path792-NumberOfMatchingSubsequences.go
More file actions
87 lines (78 loc) · 2.73 KB
/
792-NumberOfMatchingSubsequences.go
File metadata and controls
87 lines (78 loc) · 2.73 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
// 792. Number of Matching Subsequences
// 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.
import "fmt"
func numMatchingSubseq(s string, words []string) int {
res, n := 0, len(s)
for _, word := range words {
l := len(word)
if l > n {
continue
} else if l == n || l == 0 {
if word == s {
res++
}
} else {
i := 0
for j := 0; j < n; j++ {
if s[j] == word[i] {
i++
}
if i == l {
res++
break
}
}
}
}
return res
}
func numMatchingSubseq1(s string, words []string) int {
type pair struct{ i,j int }
ps := [26][]pair{} // positions,pairs
for i, w :=range words {
ps[w[0]-'a'] = append(ps[w[0]-'a'], pair{i,0})
}
res := 0
for _, c := range s {
q := ps[c-'a'] //queue
ps[c-'a'] = ps[c-'a'][:0] // 速度比ps[c-'a']=nil要快
for _, p := range q { // pair
p.j++
if p.j == len(words[p.i]) {
res++
} else {
w := words[p.i][p.j]-'a'
ps[w] = append(ps[w],p)
}
}
}
return res
}
func main() {
// 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".
fmt.Println(numMatchingSubseq("abcde", []string{"a","bb","acd","ace"})) // 3
// Example 2:
// Input: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
// Output: 2
fmt.Println(numMatchingSubseq("dsahjpjauf", []string{"ahjpjau","ja","ahbwzgqnuk","tnmlanowax"})) // 2
fmt.Println(numMatchingSubseq1("abcde", []string{"a","bb","acd","ace"})) // 3
fmt.Println(numMatchingSubseq1("dsahjpjauf", []string{"ahjpjau","ja","ahbwzgqnuk","tnmlanowax"})) // 2
}