-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringdictutil.go
57 lines (45 loc) · 1.16 KB
/
stringdictutil.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
package stringutil
import (
"sync"
)
// Sentence keeps track of all words separated
// and also tells whether it's complete (i.e. all words in dict separated and no letters left)
type Sentence struct {
Value string
}
// WordDictionary is a slice of dictionary keywords
type WordDictionary []string
// SentenceMap holds the maximum sum belonging to a particular node of a tree and below (across all possible paths).
// The node value along with the depth is kept as the hash key of the map
type SentenceMap map[int]Sentence
var mutex = &sync.Mutex{}
func (wordDict WordDictionary) matched(s string) bool {
for _, dictWord := range wordDict {
if s == dictWord {
return true
}
}
return false
}
func (wordDict WordDictionary) minLength() int {
minLength := 99999
for _, dictWord := range wordDict {
if len(dictWord) < minLength {
minLength = len(dictWord)
}
}
return minLength
}
func (hm SentenceMap) update(s string) {
mutex.Lock()
hashKey := int(Hash(s))
hm[hashKey] = Sentence{Value: s}
mutex.Unlock()
}
func (hm SentenceMap) toSlice() []string {
var slices []string
for _, item := range hm {
slices = append(slices, item.Value)
}
return slices
}