Skip to content

Commit 71d166d

Browse files
committed
Add solution and test-cases for problem 1442
1 parent 09a1dcb commit 71d166d

File tree

6 files changed

+107
-49
lines changed

6 files changed

+107
-49
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [1422.Maximum Score After Splitting a String][title]
2+
3+
## Description
4+
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).
5+
6+
The score after splitting a string is the number of **zeros** in the **left** substring plus the number of **ones** in the **right** substring.
7+
8+
**Example 1:**
9+
10+
```
11+
Input: s = "011101"
12+
Output: 5
13+
Explanation:
14+
All possible ways of splitting s into two non-empty substrings are:
15+
left = "0" and right = "11101", score = 1 + 4 = 5
16+
left = "01" and right = "1101", score = 1 + 3 = 4
17+
left = "011" and right = "101", score = 1 + 2 = 3
18+
left = "0111" and right = "01", score = 1 + 1 = 2
19+
left = "01110" and right = "1", score = 2 + 1 = 3
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: s = "00111"
26+
Output: 5
27+
Explanation: When left = "00" and right = "111", we get the maximum score = 2 + 3 = 5
28+
```
29+
30+
**Example 3:**
31+
32+
```
33+
Input: s = "1111"
34+
Output: 3
35+
```
36+
37+
## 结语
38+
39+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
40+
41+
[title]: https://leetcode.com/problems/maximum-score-after-splitting-a-string
42+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) int {
4+
l := len(s)
5+
sum := make([]int, l)
6+
sum[0] = int(s[0] - '0')
7+
8+
for idx := 1; idx < l; idx++ {
9+
sum[idx] = int(s[idx]-'0') + sum[idx-1]
10+
}
11+
12+
ans := 0
13+
for idx := 0; idx < l-1; idx++ {
14+
right := sum[l-1] - sum[idx]
15+
left := idx + 1 - sum[idx]
16+
if r := left + right; r > ans {
17+
ans = r
18+
}
19+
}
20+
return ans
521
}

leetcode/1401-1500/1422.Maximum-Score-After-Splitting-a-String/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ 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", "011101", 5},
17+
{"TestCase2", "00111", 5},
18+
{"TestCase3", "1111", 3},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

leetcode/1401-1500/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/README.md

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
11
# [1422.Maximum Score After Splitting a String][title]
22

33
## Description
4-
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).
4+
Given an array of integers `arr`.
55

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

8-
**Example 1:**
8+
Let's define a and b as follows:
99

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

22-
**Example 2:**
13+
Note that **^** denotes the **bitwise-xor** operation.
14+
15+
Return the number of triplets (`i`, `j` and `k`) Where `a == b`.
16+
17+
**Example 1:**
2318

2419
```
25-
Input: s = "00111"
26-
Output: 5
27-
Explanation: When left = "00" and right = "111", we get the maximum score = 2 + 3 = 5
20+
Input: arr = [2,3,1,6,7]
21+
Output: 4
22+
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
2823
```
2924

30-
**Example 3:**
25+
**Example 2:**
3126

3227
```
33-
Input: s = "1111"
34-
Output: 3
28+
Input: arr = [1,1,1,1,1]
29+
Output: 10
3530
```
3631

3732
## 结语
Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
package Solution
22

3-
func Solution(s string) int {
4-
l := len(s)
5-
sum := make([]int, l)
6-
sum[0] = int(s[0] - '0')
7-
8-
for idx := 1; idx < l; idx++ {
9-
sum[idx] = int(s[idx]-'0') + sum[idx-1]
10-
}
11-
12-
ans := 0
13-
for idx := 0; idx < l-1; idx++ {
14-
right := sum[l-1] - sum[idx]
15-
left := idx + 1 - sum[idx]
16-
if r := left + right; r > ans {
17-
ans = r
3+
// max(n)=300, So O(n^3) is ok.
4+
func Solution(arr []int) int {
5+
ans, xor := 0, arr[0]
6+
l := len(arr)
7+
cache := make([]int, l)
8+
cache[0] = xor
9+
for k := 1; k < l; k++ {
10+
xor ^= arr[k]
11+
now := 0
12+
for j := k; j > 0; j-- {
13+
now ^= arr[k]
14+
left := xor ^ now
15+
if left == now {
16+
ans++
17+
}
18+
for i := j - 1; i > 0; i-- {
19+
if left^cache[i-1] == now {
20+
ans++
21+
}
22+
}
1823
}
24+
cache[k] = xor
1925
}
2026
return ans
2127
}

leetcode/1401-1500/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/Solution_test.go

Lines changed: 3 additions & 4 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 string
13+
inputs []int
1414
expect int
1515
}{
16-
{"TestCase1", "011101", 5},
17-
{"TestCase2", "00111", 5},
18-
{"TestCase3", "1111", 3},
16+
{"TestCase1", []int{2, 3, 1, 6, 7}, 4},
17+
{"TestCase2", []int{1, 1, 1, 1, 1}, 10},
1918
}
2019

2120
// 开始测试

0 commit comments

Comments
 (0)