Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 2416 #992

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,48 @@
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
}
score := tree.score(w)
ans[i] = score
d[w] = score
}

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() {
}
Loading