-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatching_seq.go
56 lines (53 loc) · 1.29 KB
/
matching_seq.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
package problem0792
/*
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.
*/
func numMatchingSubseq(s string, words []string) int {
var result int
var charMap = map[byte][]int{}
for i := range s {
charMap[s[i]] = append(charMap[s[i]], i)
}
for _, word := range words {
result++
// Instant match
if word == s {
continue
}
// Instant mismatch
if len(word) >= len(s) {
result--
continue
}
if !isMatch(charMap, word) {
result--
}
}
return result
}
func isMatch(charMap map[byte][]int, word string) bool {
var curIdx, prevIdx int
// Checking if each letter is in the charMap
for i := range word {
prevIdx = curIdx
if val, ok := charMap[word[i]]; ok {
// Checking if there's a matching letter after the current index
for _, j := range val {
// Picking the closest letter to the current index
if curIdx <= j {
curIdx = j + 1
break
}
}
// If we havent found a letter after the current index
// it's a mismatch
if prevIdx == curIdx {
return false
}
} else {
return false
}
}
return true
}