Skip to content

Commit c4309b1

Browse files
committed
feat: add solutions to lc problem: No.1221. Split a String in Balanced
Strings
1 parent 81d8221 commit c4309b1

File tree

6 files changed

+177
-2
lines changed

6 files changed

+177
-2
lines changed

Diff for: solution/1200-1299/1221.Split a String in Balanced Strings/README.md

+62-1
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,76 @@
7070
<!-- 这里可写当前语言的特殊实现逻辑 -->
7171

7272
```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
7484
```
7585

7686
### **Java**
7787

7888
<!-- 这里可写当前语言的特殊实现逻辑 -->
7989

8090
```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+
```
81125
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+
}
82143
```
83144

84145
### **...**

Diff for: solution/1200-1299/1221.Split a String in Balanced Strings/README_EN.md

+62-1
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,74 @@
6060
### **Python3**
6161

6262
```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
6474
```
6575

6676
### **Java**
6777

6878
```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+
```
69113
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+
}
70131
```
71132

72133
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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

0 commit comments

Comments
 (0)