This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
Algorithms/1221.split-a-string-in-balanced-strings/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# [1221. Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/) | ||
|
||
Balanced strings are those who have equal quantity of 'L' and 'R' characters. | ||
|
||
Given a balanced string s split it in the maximum amount of balanced strings. | ||
|
||
Return the maximum amount of splitted balanced strings. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input: s = "RLRRLLRLRL" | ||
Output: 4 | ||
Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'. | ||
``` | ||
|
||
Example 2: | ||
|
||
```text | ||
Input: s = "RLLLLRRRLR" | ||
Output: 3 | ||
Explanation: s can be split into "RL", "LLLRRR", "LR", each substring contains same number of 'L' and 'R'. | ||
``` | ||
|
||
Example 3: | ||
|
||
```text | ||
Input: s = "LLLLRRRR" | ||
Output: 1 | ||
Explanation: s can be split into "LLLLRRRR". | ||
``` | ||
|
||
Constraints: | ||
|
||
- `1 <= s.length <= 1000` | ||
- `s[i] = 'L' or 'R'` |
16 changes: 16 additions & 0 deletions
16
Algorithms/1221.split-a-string-in-balanced-strings/split-a-string-in-balanced-strings.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package problem1221 | ||
|
||
func balancedStringSplit(s string) int { | ||
res, count := 0, 0 | ||
for _, b := range s { | ||
if b == 'L' { | ||
count++ | ||
} else { | ||
count-- | ||
} | ||
if count == 0 { | ||
res++ | ||
} | ||
} | ||
return res | ||
} |
47 changes: 47 additions & 0 deletions
47
...rithms/1221.split-a-string-in-balanced-strings/split-a-string-in-balanced-strings_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package problem1221 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// tcs is testcase slice | ||
var tcs = []struct { | ||
s string | ||
ans int | ||
}{ | ||
|
||
{ | ||
"RLRRLLRLRL", | ||
4, | ||
}, | ||
|
||
{ | ||
"RLLLLRRRLR", | ||
3, | ||
}, | ||
|
||
{ | ||
"LLLLRRRR", | ||
1, | ||
}, | ||
|
||
// 可以有多个 testcase | ||
} | ||
|
||
func Test_balancedStringSplit(t *testing.T) { | ||
a := assert.New(t) | ||
|
||
for _, tc := range tcs { | ||
a.Equal(tc.ans, balancedStringSplit(tc.s), "输入:%v", tc) | ||
} | ||
} | ||
|
||
func Benchmark_balancedStringSplit(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range tcs { | ||
balancedStringSplit(tc.s) | ||
} | ||
} | ||
} |
Oops, something went wrong.