Skip to content

Commit e677d63

Browse files
author
openset
committed
Add: Split a String in Balanced Strings
1 parent 4016ed7 commit e677d63

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)