-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubstring-with-concatenation-of-all-words.go
66 lines (63 loc) · 1.42 KB
/
substring-with-concatenation-of-all-words.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
package main
import "fmt"
func findSubstring(s string, words []string) []int {
var ans []int
if len(s) == 0 {
return ans
}
if len(words) == 0 {
return ans
}
all := make(map[string]int)
for _, w := range words {
if count, ok := all[w]; ok {
all[w] = count + 1
} else {
all[w] = 1
}
}
//fmt.Println("all", all)
wLen := len(words[0])
totalLen := len(words) * wLen
memo := make(map[string]int)
foundCount := 0
for i := 0; i <= len(s)-wLen; {
//fmt.Println(i, memo)
w := s[i : i+wLen]
if _, found := all[w]; found {
foundCount += 1
i += wLen
if count, ok := memo[w]; ok {
if count == all[w] {
i = i - foundCount*wLen + 1
foundCount = 0
memo = make(map[string]int)
} else {
memo[w] = count + 1
}
} else { // no memo
memo[w] = 1
}
if foundCount == len(words) {
ans = append(ans, i-totalLen)
memo = make(map[string]int)
i = i - totalLen + 1
foundCount = 0
}
} else { // not found
i = i - foundCount*wLen + 1
memo = make(map[string]int)
foundCount = 0
}
}
return ans
}
func main() {
fmt.Println(findSubstring("barfoothefoobarman", []string{"foo", "bar"}))
fmt.Println(findSubstring("barfoofoobarthefoobarman",
[]string{"foo", "bar", "the"}))
fmt.Println(findSubstring("wordgoodgoodgoodbestword",
[]string{"word", "good", "best", "good"}))
fmt.Println(findSubstring("aabbaabbaabb",
[]string{"bb", "aa", "bb", "aa", "bb"}))
}