Skip to content

Commit 20a9e6d

Browse files
authored
Merge pull request #884 from 0xff-dev/1781
Add solution and test-cases for problem 1781
2 parents 5e7b676 + 3a9e79d commit 20a9e6d

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

leetcode/1701-1800/1781.Sum-of-Beauty-of-All-Substrings/README.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
# [1781.Sum of Beauty of All Substrings][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
The **beauty** of a string is the difference in frequencies between the most frequent and least frequent characters.
5+
6+
- For example, the beauty of `"abaacc"` is `3 - 1 = 2`.
7+
8+
Given a string `s`, return the sum of **beauty** of all of its substrings.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: s = "aabcb"
14+
Output: 5
15+
Explanation: The substrings with non-zero beauty are ["aab","aabc","aabcb","abcb","bcb"], each with beauty equal to 1.
1316
```
1417

15-
## 题意
16-
> ...
18+
**Example 2:**
1719

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Sum of Beauty of All Substrings
23-
```go
2420
```
25-
21+
Input: s = "aabcbaa"
22+
Output: 17
23+
```
2624

2725
## 结语
2826

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) int {
4+
ans := 0
5+
//ab,aa
6+
for end := 2; end < len(s); end++ {
7+
tmp := [26]int{}
8+
tmp[s[end]-'a']++
9+
a := 0
10+
for pre := end - 1; pre >= 0; pre-- {
11+
b := 0
12+
tmp[s[pre]-'a']++
13+
a = max(a, tmp[s[pre]-'a'])
14+
for i := 0; i < 26; i++ {
15+
if tmp[i] != 0 {
16+
if b == 0 || b > tmp[i] {
17+
b = tmp[i]
18+
}
19+
}
20+
}
21+
ans += a - b
22+
}
23+
}
24+
return ans
525
}

leetcode/1701-1800/1781.Sum-of-Beauty-of-All-Substrings/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "aabcb", 5},
17+
{"TestCase2", "aabcbaa", 17},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)