Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 6c28e5b

Browse files
committed
1221 done
1 parent c932935 commit 6c28e5b

File tree

5 files changed

+137
-32
lines changed

5 files changed

+137
-32
lines changed

.vscode/settings.json

+6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
"LLLDDDDD",
3333
"LLLLD",
3434
"LLLLDDD",
35+
"LLLLRRRR",
3536
"LLLLUUUU",
37+
"LLLRRR",
3638
"LLLUU",
3739
"LLUU",
3840
"Leet",
@@ -42,7 +44,10 @@
4244
"Paddr",
4345
"Puerkito",
4446
"Qedo",
47+
"RLLLLRRRLR",
48+
"RLRRLLRLRL",
4549
"RRDD",
50+
"RRLL",
4651
"RRRD",
4752
"RRRRU",
4853
"RRRU",
@@ -248,6 +253,7 @@
248253
"size",
249254
"sort",
250255
"split",
256+
"splitted",
251257
"ssaaedd",
252258
"stretchr",
253259
"string",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [1221. Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/)
2+
3+
Balanced strings are those who have equal quantity of 'L' and 'R' characters.
4+
5+
Given a balanced string s split it in the maximum amount of balanced strings.
6+
7+
Return the maximum amount of splitted balanced strings.
8+
9+
Example 1:
10+
11+
```text
12+
Input: s = "RLRRLLRLRL"
13+
Output: 4
14+
Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'.
15+
```
16+
17+
Example 2:
18+
19+
```text
20+
Input: s = "RLLLLRRRLR"
21+
Output: 3
22+
Explanation: s can be split into "RL", "LLLRRR", "LR", each substring contains same number of 'L' and 'R'.
23+
```
24+
25+
Example 3:
26+
27+
```text
28+
Input: s = "LLLLRRRR"
29+
Output: 1
30+
Explanation: s can be split into "LLLLRRRR".
31+
```
32+
33+
Constraints:
34+
35+
- `1 <= s.length <= 1000`
36+
- `s[i] = 'L' or 'R'`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package problem1221
2+
3+
func balancedStringSplit(s string) int {
4+
res, count := 0, 0
5+
for _, b := range s {
6+
if b == 'L' {
7+
count++
8+
} else {
9+
count--
10+
}
11+
if count == 0 {
12+
res++
13+
}
14+
}
15+
return res
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package problem1221
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// tcs is testcase slice
10+
var tcs = []struct {
11+
s string
12+
ans int
13+
}{
14+
15+
{
16+
"RLRRLLRLRL",
17+
4,
18+
},
19+
20+
{
21+
"RLLLLRRRLR",
22+
3,
23+
},
24+
25+
{
26+
"LLLLRRRR",
27+
1,
28+
},
29+
30+
// 可以有多个 testcase
31+
}
32+
33+
func Test_balancedStringSplit(t *testing.T) {
34+
a := assert.New(t)
35+
36+
for _, tc := range tcs {
37+
a.Equal(tc.ans, balancedStringSplit(tc.s), "输入:%v", tc)
38+
}
39+
}
40+
41+
func Benchmark_balancedStringSplit(b *testing.B) {
42+
for i := 0; i < b.N; i++ {
43+
for _, tc := range tcs {
44+
balancedStringSplit(tc.s)
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)