File tree 6 files changed +177
-2
lines changed
solution/1200-1299/1221.Split a String in Balanced Strings
6 files changed +177
-2
lines changed Original file line number Diff line number Diff line change 70
70
<!-- 这里可写当前语言的特殊实现逻辑 -->
71
71
72
72
``` python
73
-
73
+ class Solution :
74
+ def balancedStringSplit (self , s : str ) -> int :
75
+ n = res = 0
76
+ for c in s:
77
+ if c == ' L' :
78
+ n += 1
79
+ else :
80
+ n -= 1
81
+ if n == 0 :
82
+ res += 1
83
+ return res
74
84
```
75
85
76
86
### ** Java**
77
87
78
88
<!-- 这里可写当前语言的特殊实现逻辑 -->
79
89
80
90
``` java
91
+ class Solution {
92
+ public int balancedStringSplit (String s ) {
93
+ int n = 0 , res = 0 ;
94
+ for (char c : s. toCharArray()) {
95
+ if (c == ' L' ) {
96
+ ++ n;
97
+ } else {
98
+ -- n;
99
+ }
100
+ if (n == 0 ) {
101
+ ++ res;
102
+ }
103
+ }
104
+ return res;
105
+ }
106
+ }
107
+ ```
108
+
109
+ ### ** C++**
110
+
111
+ ``` cpp
112
+ class Solution {
113
+ public:
114
+ int balancedStringSplit(string s) {
115
+ int n = 0, res = 0;
116
+ for (char c : s) {
117
+ if (c == 'L') ++n;
118
+ else --n;
119
+ if (n == 0) ++res;
120
+ }
121
+ return res;
122
+ }
123
+ };
124
+ ```
81
125
126
+ ### **Go**
127
+
128
+ ```go
129
+ func balancedStringSplit(s string) int {
130
+ n, res := 0, 0
131
+ for _, c := range s {
132
+ if c == 'L' {
133
+ n++
134
+ } else {
135
+ n--
136
+ }
137
+ if n == 0 {
138
+ res++
139
+ }
140
+ }
141
+ return res
142
+ }
82
143
```
83
144
84
145
### ** ...**
Original file line number Diff line number Diff line change 60
60
### ** Python3**
61
61
62
62
``` python
63
-
63
+ class Solution :
64
+ def balancedStringSplit (self , s : str ) -> int :
65
+ n = res = 0
66
+ for c in s:
67
+ if c == ' L' :
68
+ n += 1
69
+ else :
70
+ n -= 1
71
+ if n == 0 :
72
+ res += 1
73
+ return res
64
74
```
65
75
66
76
### ** Java**
67
77
68
78
``` java
79
+ class Solution {
80
+ public int balancedStringSplit (String s ) {
81
+ int n = 0 , res = 0 ;
82
+ for (char c : s. toCharArray()) {
83
+ if (c == ' L' ) {
84
+ ++ n;
85
+ } else {
86
+ -- n;
87
+ }
88
+ if (n == 0 ) {
89
+ ++ res;
90
+ }
91
+ }
92
+ return res;
93
+ }
94
+ }
95
+ ```
96
+
97
+ ### ** C++**
98
+
99
+ ``` cpp
100
+ class Solution {
101
+ public:
102
+ int balancedStringSplit(string s) {
103
+ int n = 0, res = 0;
104
+ for (char c : s) {
105
+ if (c == 'L') ++n;
106
+ else --n;
107
+ if (n == 0) ++res;
108
+ }
109
+ return res;
110
+ }
111
+ };
112
+ ```
69
113
114
+ ### **Go**
115
+
116
+ ```go
117
+ func balancedStringSplit(s string) int {
118
+ n, res := 0, 0
119
+ for _, c := range s {
120
+ if c == 'L' {
121
+ n++
122
+ } else {
123
+ n--
124
+ }
125
+ if n == 0 {
126
+ res++
127
+ }
128
+ }
129
+ return res
130
+ }
70
131
```
71
132
72
133
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int balancedStringSplit (string s) {
4
+ int n = 0 , res = 0 ;
5
+ for (char c : s) {
6
+ if (c == ' L' ) ++n;
7
+ else --n;
8
+ if (n == 0 ) ++res;
9
+ }
10
+ return res;
11
+ }
12
+ };
Original file line number Diff line number Diff line change
1
+ func balancedStringSplit (s string ) int {
2
+ n , res := 0 , 0
3
+ for _ , c := range s {
4
+ if c == 'L' {
5
+ n ++
6
+ } else {
7
+ n --
8
+ }
9
+ if n == 0 {
10
+ res ++
11
+ }
12
+ }
13
+ return res
14
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int balancedStringSplit (String s ) {
3
+ int n = 0 , res = 0 ;
4
+ for (char c : s .toCharArray ()) {
5
+ if (c == 'L' ) {
6
+ ++n ;
7
+ } else {
8
+ --n ;
9
+ }
10
+ if (n == 0 ) {
11
+ ++res ;
12
+ }
13
+ }
14
+ return res ;
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def balancedStringSplit (self , s : str ) -> int :
3
+ n = res = 0
4
+ for c in s :
5
+ if c == 'L' :
6
+ n += 1
7
+ else :
8
+ n -= 1
9
+ if n == 0 :
10
+ res += 1
11
+ return res
You can’t perform that action at this time.
0 commit comments