Skip to content

Commit 40d6077

Browse files
committed
Add solution and test-cases for problem 1525
1 parent 09a1dcb commit 40d6077

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

leetcode/1501-1600/1525.Number-of-Good-Ways-to-Split-a-String/README.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [1525.Number of Good Ways to Split a String][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+
You are given a string `s`.
5+
6+
A split is called **good** if you can split `s` into two non-empty strings s<sub>left</sub> and s<sub>right</sub> where their concatenation is equal to `s` (i.e., s<sub>left</sub> + s<sub>right</sub> = `s`) and the number of distinct letters in s<sub>left</sub> and s<sub>right</sub> is the same.
7+
8+
Return the number of **good splits** you can make in `s`.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: s = "aacaba"
14+
Output: 2
15+
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
16+
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
17+
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
18+
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
19+
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
20+
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Number of Good Ways to Split a String
23-
```go
2425
```
25-
26+
Input: s = "abcd"
27+
Output: 1
28+
Explanation: Split the string as follows ("ab", "cd").
29+
```
2630

2731
## 结语
2832

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) int {
4+
ans := 0
5+
total := [26]int{}
6+
for _, b := range s {
7+
total[b-'a']++
8+
}
9+
left := [26]int{}
10+
for end := 0; end < len(s)-1; end++ {
11+
left[s[end]-'a']++
12+
a, b := 0, 0
13+
for i := 0; i < 26; i++ {
14+
if left[i] > 0 {
15+
a++
16+
}
17+
if total[i] > left[i] {
18+
b++
19+
}
20+
}
21+
if a == b {
22+
ans++
23+
}
24+
}
25+
return ans
526
}

leetcode/1501-1600/1525.Number-of-Good-Ways-to-Split-a-String/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", "aacaba", 2},
17+
{"TestCase2", "abcd", 1},
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)