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

Commit

Permalink
1221 done
Browse files Browse the repository at this point in the history
  • Loading branch information
aQuaYi committed Oct 31, 2019
1 parent c932935 commit 6c28e5b
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 32 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"LLLDDDDD",
"LLLLD",
"LLLLDDD",
"LLLLRRRR",
"LLLLUUUU",
"LLLRRR",
"LLLUU",
"LLUU",
"Leet",
Expand All @@ -42,7 +44,10 @@
"Paddr",
"Puerkito",
"Qedo",
"RLLLLRRRLR",
"RLRRLLRLRL",
"RRDD",
"RRLL",
"RRRD",
"RRRRU",
"RRRU",
Expand Down Expand Up @@ -248,6 +253,7 @@
"size",
"sort",
"split",
"splitted",
"ssaaedd",
"stretchr",
"string",
Expand Down
36 changes: 36 additions & 0 deletions Algorithms/1221.split-a-string-in-balanced-strings/README.md
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'`
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
}
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)
}
}
}
Loading

0 comments on commit 6c28e5b

Please sign in to comment.