deno.land / x / masx200_leetcode_test@10.6.5 / prefix-and-suffix-search / index.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 index
import "sort"
type WordFilter struct { d map[string]int}type Entry struct { Key string Value int}
func Constructor(words []string) WordFilter {
m := map[string]int{} for i, word := range words {
m[word] = i }
entries := EntriesMap(m) sort.Slice(entries, func(i, j int) bool {
return entries[i].Value < entries[j].Value }) d := map[string]int{} for _, entry := range entries { word := entry.Key i := entry.Value n := len(word) for j := 1; j <= n; j++ { for k := 1; k <= n; k++ { d[word[:j]+","+word[n-k:]] = i } } } return WordFilter{d: d}}
func EntriesMap(m map[string]int) []Entry { entries := []Entry{} for k, v := range m { entries = append(entries, Entry{Key: k, Value: v}) } return entries}
func (w *WordFilter) F(pref string, suff string) int { v, ok := w.d[pref+","+suff] if ok { return v } else { return -1 }}
masx200_leetcode_test

Version Info

Tagged at
a year ago