-
Notifications
You must be signed in to change notification settings - Fork 0
/
030.go
85 lines (76 loc) · 1.85 KB
/
030.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package p030
//import "fmt"
/**
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.
For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]
You should return the indices: [0,9].
(order does not matter).
*/
// words 会包括重复项
func findSubstring(s string, words []string) []int {
ans := make([]int, 0)
wordsLen := len(words)
if wordsLen <= 0 {
return ans
}
wordSize := len(words[0])
wordsMap := make(map[string]int)
wordsCount := make([]int, wordsLen)
for i, w := range words {
v, ok := wordsMap[w]
if !ok {
wordsMap[w] = i
v = i
}
wordsCount[v]++
}
bs := []byte(s)
for i := 0; i < wordSize; i++ {
findSeq := make([]int, 0)
findIndex := -1
hitCount := make([]int, wordsLen)
findCount := 0
for j := i; j < len(bs)-wordSize+1; j = j + wordSize {
if v, ok := wordsMap[string(bs[j:j+wordSize])]; ok {
if hitCount[v] == wordsCount[v] {
for findSeq[0] != v {
findCount--
hitCount[findSeq[0]]--
findSeq = findSeq[1:]
findIndex += wordSize
}
findCount--
findSeq = findSeq[1:]
findIndex += wordSize
} else {
hitCount[v]++
}
findCount++
findSeq = append(findSeq, v)
if findIndex == -1 {
findIndex = j
}
//fmt.Println("j", j, "v", v, "findseq", findSeq)
//fmt.Println(hitCount)
} else { // not a word
findCount = 0
findSeq = findSeq[0:0]
for k := 0; k < wordsLen; k++ {
hitCount[k] = 0
}
findIndex = -1
}
if findCount == wordsLen {
ans = append(ans, findIndex)
//fmt.Println("append ans", findIndex)
findCount--
hitCount[findSeq[0]]--
findSeq = findSeq[1:]
findIndex += wordSize
}
}
}
return ans
}