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

Commit 46d867c

Browse files
committed
合并更改
2 parents 7dc9a95 + 34d4e50 commit 46d867c

File tree

7 files changed

+178
-41
lines changed

7 files changed

+178
-41
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+
}

Favorite.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@
235235
|[0839](https://leetcode.com/problems/similar-string-groups/)|[Similar String Groups](./Algorithms/0839.similar-string-groups)|36%|Hard|[](https://leetcode.com/list/oussv5j)|
236236
|[0843](https://leetcode.com/problems/guess-the-word/)|[Guess the Word](./Algorithms/0843.guess-the-word)|45%|Hard|[](https://leetcode.com/list/oussv5j)|
237237
|[0846](https://leetcode.com/problems/hand-of-straights/)|[Hand of Straights](./Algorithms/0846.hand-of-straights)|50%|Medium|[](https://leetcode.com/list/oussv5j)|
238-
|[0847](https://leetcode.com/problems/shortest-path-visiting-all-nodes/)|[Shortest Path Visiting All Nodes](./Algorithms/0847.shortest-path-visiting-all-nodes)|48%|Hard|[](https://leetcode.com/list/oussv5j)|
238+
|[0847](https://leetcode.com/problems/shortest-path-visiting-all-nodes/)|[Shortest Path Visiting All Nodes](./Algorithms/0847.shortest-path-visiting-all-nodes)|49%|Hard|[](https://leetcode.com/list/oussv5j)|
239239
|[0849](https://leetcode.com/problems/maximize-distance-to-closest-person/)|[Maximize Distance to Closest Person](./Algorithms/0849.maximize-distance-to-closest-person)|41%|Easy|[](https://leetcode.com/list/oussv5j)|
240240
|[0850](https://leetcode.com/problems/rectangle-area-ii/)|[Rectangle Area II](./Algorithms/0850.rectangle-area-ii)|46%|Hard|[](https://leetcode.com/list/oussv5j)|
241241
|[0851](https://leetcode.com/problems/loud-and-rich/)|[Loud and Rich](./Algorithms/0851.loud-and-rich)|49%|Medium|[](https://leetcode.com/list/oussv5j)|
@@ -270,7 +270,7 @@
270270
|[0939](https://leetcode.com/problems/minimum-area-rectangle/)|[Minimum Area Rectangle](./Algorithms/0939.minimum-area-rectangle)|51%|Medium|[](https://leetcode.com/list/oussv5j)|
271271
|[0940](https://leetcode.com/problems/distinct-subsequences-ii/)|[Distinct Subsequences II](./Algorithms/0940.distinct-subsequences-ii)|40%|Hard|[](https://leetcode.com/list/oussv5j)|
272272
|[0943](https://leetcode.com/problems/find-the-shortest-superstring/)|[Find the Shortest Superstring](./Algorithms/0943.find-the-shortest-superstring)|40%|Hard|[](https://leetcode.com/list/oussv5j)|
273-
|[0945](https://leetcode.com/problems/minimum-increment-to-make-array-unique/)|[Minimum Increment to Make Array Unique](./Algorithms/0945.minimum-increment-to-make-array-unique)|44%|Medium|[](https://leetcode.com/list/oussv5j)|
273+
|[0945](https://leetcode.com/problems/minimum-increment-to-make-array-unique/)|[Minimum Increment to Make Array Unique](./Algorithms/0945.minimum-increment-to-make-array-unique)|43%|Medium|[](https://leetcode.com/list/oussv5j)|
274274
|[0947](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/)|[Most Stones Removed with Same Row or Column](./Algorithms/0947.most-stones-removed-with-same-row-or-column)|54%|Medium|[](https://leetcode.com/list/oussv5j)|
275275
|[0948](https://leetcode.com/problems/bag-of-tokens/)|[Bag of Tokens](./Algorithms/0948.bag-of-tokens)|39%|Medium|[](https://leetcode.com/list/oussv5j)|
276276
|[0949](https://leetcode.com/problems/largest-time-for-given-digits/)|[Largest Time for Given Digits](./Algorithms/0949.largest-time-for-given-digits)|34%|Easy|[](https://leetcode.com/list/oussv5j)|
@@ -340,7 +340,7 @@
340340
|[1145](https://leetcode.com/problems/binary-tree-coloring-game/)|[Binary Tree Coloring Game](./Algorithms/1145.binary-tree-coloring-game)|49%|Medium|[](https://leetcode.com/list/oussv5j)|
341341
|[1146](https://leetcode.com/problems/snapshot-array/)|[Snapshot Array](./Algorithms/1146.snapshot-array)|34%|Medium|[](https://leetcode.com/list/oussv5j)|
342342
|[1155](https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/)|[Number of Dice Rolls With Target Sum](./Algorithms/1155.number-of-dice-rolls-with-target-sum)|48%|Medium|[](https://leetcode.com/list/oussv5j)|
343-
|[1157](https://leetcode.com/problems/online-majority-element-in-subarray/)|[Online Majority Element In Subarray](./Algorithms/1157.online-majority-element-in-subarray)|33%|Hard|[](https://leetcode.com/list/oussv5j)|
343+
|[1157](https://leetcode.com/problems/online-majority-element-in-subarray/)|[Online Majority Element In Subarray](./Algorithms/1157.online-majority-element-in-subarray)|34%|Hard|[](https://leetcode.com/list/oussv5j)|
344344
|[1163](https://leetcode.com/problems/last-substring-in-lexicographical-order/)|[Last Substring in Lexicographical Order](./Algorithms/1163.last-substring-in-lexicographical-order)|30%|Hard|[](https://leetcode.com/list/oussv5j)|
345345
|[1172](https://leetcode.com/problems/dinner-plate-stacks/)|[Dinner Plate Stacks](./Algorithms/1172.dinner-plate-stacks)|40%|Hard|[](https://leetcode.com/list/oussv5j)|
346346
|[1177](https://leetcode.com/problems/can-make-palindrome-from-substring/)|[Can Make Palindrome from Substring](./Algorithms/1177.can-make-palindrome-from-substring)|32%|Medium|[](https://leetcode.com/list/oussv5j)|

0 commit comments

Comments
 (0)