Skip to content

Commit

Permalink
Merge pull request #852 from 0xff-dev/1525
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1525
  • Loading branch information
6boris authored Jun 6, 2024
2 parents ffe3acc + 40d6077 commit 022b1b0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
# [1525.Number of Good Ways to Split a String][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 a string `s`.

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.

Return the number of **good splits** you can make in `s`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
```

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

## 题解

### 思路1
> ...
Number of Good Ways to Split a String
```go
```

Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
```

## 结语

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

func Solution(x bool) bool {
return x
func Solution(s string) int {
ans := 0
total := [26]int{}
for _, b := range s {
total[b-'a']++
}
left := [26]int{}
for end := 0; end < len(s)-1; end++ {
left[s[end]-'a']++
a, b := 0, 0
for i := 0; i < 26; i++ {
if left[i] > 0 {
a++
}
if total[i] > left[i] {
b++
}
}
if a == b {
ans++
}
}
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", "aacaba", 2},
{"TestCase2", "abcd", 1},
}

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

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

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

0 comments on commit 022b1b0

Please sign in to comment.