-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path139. Word Break.go
More file actions
52 lines (38 loc) · 767 Bytes
/
139. Word Break.go
File metadata and controls
52 lines (38 loc) · 767 Bytes
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
package main
import (
"fmt"
)
func wordBreak(s string, wordDict []string) bool {
dict := map[string]bool{}
for i := 0; i < len(wordDict); i++ {
dict[wordDict[i]] = false
}
word := ""
wordCount := 0
for i := 0; i < len(s); i++ {
word = word + string(s[i])
if _, didFind := dict[word]; didFind {
dict[word] = true
wordCount += len(word)
word = ""
}
}
if wordCount == len(s) {
return true
}
fmt.Println(dict)
for _, val := range dict {
if val == false {
return false
}
}
return true
}
func main() {
// Input: s = "leetcode",
// Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
// "aaaaaaa"
// ["aaaa","aaa"]
wordDict := []string{"aaaa", "aaa"}
fmt.Println(wordBreak("aaaaaaa", wordDict))
}