Skip to content

Commit

Permalink
Add solution and test-cases for problem 2416
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Sep 25, 2024
1 parent ffaca5d commit 795520e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 22 deletions.
45 changes: 31 additions & 14 deletions leetcode/2401-2500/2416.Sum-of-Prefix-Scores-of-Strings/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
# [2416.Sum of Prefix Scores of Strings][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
You are given an array `words` of size `n` consisting of **non-empty** strings.

We define the **score** of a string `word` as the **number** of strings `words[i]` such that `word` is a **prefix** of `words[i]`.

- For example, if `words = ["a", "ab", "abc", "cab"]`, then the score of `"ab"` is `2`, since `"ab"` is a prefix of both `"ab"` and `"abc"`.

Return an array `answer` of size `n` where `answer[i]` is the **sum** of scores of every **non-empty** prefix of `words[i]`.

**Note** that a string is considered as a prefix of itself.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: words = ["abc","ab","bc","b"]
Output: [5,4,3,2]
Explanation: The answer for each string is the following:
- "abc" has 3 prefixes: "a", "ab", and "abc".
- There are 2 strings with the prefix "a", 2 strings with the prefix "ab", and 1 string with the prefix "abc".
The total is answer[0] = 2 + 2 + 1 = 5.
- "ab" has 2 prefixes: "a" and "ab".
- There are 2 strings with the prefix "a", and 2 strings with the prefix "ab".
The total is answer[1] = 2 + 2 = 4.
- "bc" has 2 prefixes: "b" and "bc".
- There are 2 strings with the prefix "b", and 1 string with the prefix "bc".
The total is answer[2] = 2 + 1 = 3.
- "b" has 1 prefix: "b".
- There are 2 strings with the prefix "b".
The total is answer[3] = 2.
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Sum of Prefix Scores of Strings
```go
```

Input: words = ["abcd"]
Output: [4]
Explanation:
"abcd" has 4 prefixes: "a", "ab", "abc", and "abcd".
Each prefix has a score of one, so the total is answer[0] = 1 + 1 + 1 + 1 = 4.
```

## 结语

Expand Down
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []string
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []string{"abc", "ab", "bc", "b"}, []int{5, 4, 3, 2}},
{"TestCase2", []string{"abcd"}, []int{4}},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 795520e

Please sign in to comment.