Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c6ec45f

Browse files
author
Shuo
authored
Merge pull request #694 from openset/develop
Add: Split a String in Balanced Strings
2 parents 3cd9fc9 + e677d63 commit c6ec45f

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package problem_1221
2+
3+
func balancedStringSplit(s string) int {
4+
ans, flag := 0, 0
5+
for _, b := range s {
6+
if b == 'L' {
7+
flag--
8+
} else {
9+
flag++
10+
}
11+
if flag == 0 {
12+
ans++
13+
}
14+
}
15+
return ans
16+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package problem_1221
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input string
7+
expected int
8+
}
9+
10+
func TestBalancedStringSplit(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: "RLRRLLRLRL",
14+
expected: 4,
15+
},
16+
{
17+
input: "RLLLLRRRLR",
18+
expected: 3,
19+
},
20+
{
21+
input: "LLLLRRRR",
22+
expected: 1,
23+
},
24+
}
25+
for _, tc := range tests {
26+
output := balancedStringSplit(tc.input)
27+
if output != tc.expected {
28+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
29+
}
30+
}
31+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /