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 a30b0b4

Browse files
committed
1221
1 parent ea02477 commit a30b0b4

File tree

1 file changed

+28
-0
lines changed
  • Easy/1221. Split a String in Balanced Strings

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func balancedStringSplit(s string) int {
6+
balance := 0
7+
count := 0
8+
9+
for _, ch := range s {
10+
switch ch {
11+
case 'R':
12+
balance++
13+
case 'L':
14+
balance--
15+
}
16+
17+
if balance == 0 {
18+
count++
19+
}
20+
}
21+
22+
return count
23+
}
24+
25+
func main() {
26+
s := "RLRRLLRLRL"
27+
fmt.Println(balancedStringSplit(s)) // Output: 4
28+
}

0 commit comments

Comments
(0)

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