funcsumPrefixScores(words []string) []int { res := make([]int, len(words)) hmap := map[string]int{} prefix := make([]strings.Builder, len(words)) for i := 0; i < 1000; i++ { for j := 0; j < len(words); j++ { if i >= len(words[j]) { continue } prefix[j].WriteByte(words[j][i]) hmap[prefix[j].String()]++ } for j := 0; j < len(words); j++ { if i >= len(words[j]) { continue } if val, ok := hmap[prefix[j].String()]; ok { res[j] += val } } } return res }
func(t *Trie) Add(s string) { curT := t curT.val++ for i := 0; i < len(s); i++ { if curT.subTire == nil { curT.subTire = make([]Trie, 26) } curT = &curT.subTire[int(s[i]-'a')] curT.val++ } }
funcsumPrefixScores(words []string) []int { trie := newTrie() for _, word := range words { trie.Add(word) } res := make([]int, len(words)) for index, word := range words { curT := trie for i := 0; i < len(word); i++ { curT = &curT.subTire[int(word[i]-'a')] res[index] += curT.val } } return res }