Skip to content

Commit 2717a66

Browse files
authored
Create split-a-string-in-balanced-strings.cpp
1 parent ed727f8 commit 2717a66

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int balancedStringSplit(string s) {
7+
int result = 0, count = 0;
8+
for (const auto& c : s) {
9+
count += (c == 'L') ? 1 : -1;
10+
if (count == 0) {
11+
++result;
12+
}
13+
}
14+
return result;
15+
}
16+
};

0 commit comments

Comments
 (0)