Skip to content

Add solution and test-cases for problem 1442 #868

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

Merged
merged 1 commit into from
Jun 6, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# [1422.Maximum Score After Splitting a String][title]

## Description
Given a string `s` of zeros and ones, return the maximum score after splitting the string into two **non-empty** substrings (i.e. **left** substring and **right** substring).

The score after splitting a string is the number of **zeros** in the **left** substring plus the number of **ones** in the **right** substring.

**Example 1:**

```
Input: s = "011101"
Output: 5
Explanation:
All possible ways of splitting s into two non-empty substrings are:
left = "0" and right = "11101", score = 1 + 4 = 5
left = "01" and right = "1101", score = 1 + 3 = 4
left = "011" and right = "101", score = 1 + 2 = 3
left = "0111" and right = "01", score = 1 + 1 = 2
left = "01110" and right = "1", score = 2 + 1 = 3
```

**Example 2:**

```
Input: s = "00111"
Output: 5
Explanation: When left = "00" and right = "111", we get the maximum score = 2 + 3 = 5
```

**Example 3:**

```
Input: s = "1111"
Output: 3
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/maximum-score-after-splitting-a-string
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string) int {
l := len(s)
sum := make([]int, l)
sum[0] = int(s[0] - '0')

for idx := 1; idx < l; idx++ {
sum[idx] = int(s[idx]-'0') + sum[idx-1]
}

ans := 0
for idx := 0; idx < l-1; idx++ {
right := sum[l-1] - sum[idx]
left := idx + 1 - sum[idx]
if r := left + right; r > ans {
ans = r
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ 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", "011101", 5},
{"TestCase2", "00111", 5},
{"TestCase3", "1111", 3},
}

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

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

// 使用案列
// 使用案列
func ExampleSolution() {
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
# [1422.Maximum Score After Splitting a String][title]

## Description
Given a string `s` of zeros and ones, return the maximum score after splitting the string into two **non-empty** substrings (i.e. **left** substring and **right** substring).
Given an array of integers `arr`.

The score after splitting a string is the number of **zeros** in the **left** substring plus the number of **ones** in the **right** substring.
We want to select three indices `i`, `j` and k where `(0 <= i < j <= k < arr.length)`.

**Example 1:**
Let's define a and b as follows:

```
Input: s = "011101"
Output: 5
Explanation:
All possible ways of splitting s into two non-empty substrings are:
left = "0" and right = "11101", score = 1 + 4 = 5
left = "01" and right = "1101", score = 1 + 3 = 4
left = "011" and right = "101", score = 1 + 2 = 3
left = "0111" and right = "01", score = 1 + 1 = 2
left = "01110" and right = "1", score = 2 + 1 = 3
```
- `a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]`
- `b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]`

**Example 2:**
Note that **^** denotes the **bitwise-xor** operation.

Return the number of triplets (`i`, `j` and `k`) Where `a == b`.

**Example 1:**

```
Input: s = "00111"
Output: 5
Explanation: When left = "00" and right = "111", we get the maximum score = 2 + 3 = 5
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
```

**Example 3:**
**Example 2:**

```
Input: s = "1111"
Output: 3
Input: arr = [1,1,1,1,1]
Output: 10
```

## 结语
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package Solution

func Solution(s string) int {
l := len(s)
sum := make([]int, l)
sum[0] = int(s[0] - '0')

for idx := 1; idx < l; idx++ {
sum[idx] = int(s[idx]-'0') + sum[idx-1]
}

ans := 0
for idx := 0; idx < l-1; idx++ {
right := sum[l-1] - sum[idx]
left := idx + 1 - sum[idx]
if r := left + right; r > ans {
ans = r
// max(n)=300, So O(n^3) is ok.
func Solution(arr []int) int {
ans, xor := 0, arr[0]
l := len(arr)
cache := make([]int, l)
cache[0] = xor
for k := 1; k < l; k++ {
xor ^= arr[k]
now := 0
for j := k; j > 0; j-- {
now ^= arr[k]
left := xor ^ now
if left == now {
ans++
}
for i := j - 1; i > 0; i-- {
if left^cache[i-1] == now {
ans++
}
}
}
cache[k] = xor
}
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 string
inputs []int
expect int
}{
{"TestCase1", "011101", 5},
{"TestCase2", "00111", 5},
{"TestCase3", "1111", 3},
{"TestCase1", []int{2, 3, 1, 6, 7}, 4},
{"TestCase2", []int{1, 1, 1, 1, 1}, 10},
}

// 开始测试
Expand Down