Skip to content

Commit

Permalink
Merge pull request #884 from 0xff-dev/1781
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1781
  • Loading branch information
6boris committed Jun 6, 2024
2 parents 5e7b676 + 3a9e79d commit 20a9e6d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
26 changes: 12 additions & 14 deletions leetcode/1701-1800/1781.Sum-of-Beauty-of-All-Substrings/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
# [1781.Sum of Beauty of All Substrings][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
The **beauty** of a string is the difference in frequencies between the most frequent and least frequent characters.

- For example, the beauty of `"abaacc"` is `3 - 1 = 2`.

Given a string `s`, return the sum of **beauty** of all of its substrings.

**Example 1:**

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

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

## 题解

### 思路1
> ...
Sum of Beauty of All Substrings
```go
```

Input: s = "aabcbaa"
Output: 17
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string) int {
ans := 0
//ab,aa
for end := 2; end < len(s); end++ {
tmp := [26]int{}
tmp[s[end]-'a']++
a := 0
for pre := end - 1; pre >= 0; pre-- {
b := 0
tmp[s[pre]-'a']++
a = max(a, tmp[s[pre]-'a'])
for i := 0; i < 26; i++ {
if tmp[i] != 0 {
if b == 0 || b > tmp[i] {
b = tmp[i]
}
}
}
ans += a - b
}
}
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", "aabcb", 5},
{"TestCase2", "aabcbaa", 17},
}

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

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

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

0 comments on commit 20a9e6d

Please sign in to comment.