-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution and test-cases for problem 2416
- Loading branch information
Showing
3 changed files
with
79 additions
and
22 deletions.
There are no files selected for viewing
45 changes: 31 additions & 14 deletions
45
leetcode/2401-2500/2416.Sum-of-Prefix-Scores-of-Strings/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 42 additions & 1 deletion
43
leetcode/2401-2500/2416.Sum-of-Prefix-Scores-of-Strings/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,46 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
type trieNode2416 struct { | ||
count int | ||
child [26]*trieNode2416 | ||
} | ||
|
||
func (r *trieNode2416) insert(word string) { | ||
walker := r | ||
for i := range word { | ||
index := word[i] - 'a' | ||
if walker.child[index] == nil { | ||
walker.child[index] = &trieNode2416{} | ||
} | ||
walker.child[index].count++ | ||
walker = walker.child[index] | ||
} | ||
} | ||
|
||
func (r *trieNode2416) score(word string) int { | ||
walker := r | ||
x := 0 | ||
for i := range word { | ||
index := word[i] - 'a' | ||
x += walker.child[index].count | ||
walker = walker.child[index] | ||
} | ||
return x | ||
} | ||
func Solution(words []string) []int { | ||
ans := make([]int, len(words)) | ||
tree := &trieNode2416{} | ||
for _, w := range words { | ||
tree.insert(w) | ||
} | ||
d := make(map[string]int) | ||
for i, w := range words { | ||
if v, ok := d[w]; ok { | ||
ans[i] = v | ||
continue | ||
} | ||
ans[i] = tree.score(w) | ||
} | ||
|
||
return ans | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters