-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbalancedStringSplit.js
More file actions
44 lines (33 loc) · 878 Bytes
/
balancedStringSplit.js
File metadata and controls
44 lines (33 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
https://leetcode.com/problems/split-a-string-in-balanced-strings/
Balanced strings are those who have equal quantity of 'L' and 'R' characters.
Given a balanced string s split it in the maximum amount of balanced strings.
Return the maximum amount of splitted balanced strings.
Example 1:
Input: s = "RLRRLLRLRL"
Output: 4
Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'.
*/
var balancedStringSplit = function (s) {
let res = [];
let rCount = 0;
let lCount = 0;
let newStr = "";
for (let i = 0; i < s.length; i++) {
if (s[i] === "L") {
lCount++;
newStr += "L";
}
if (s[i] === "R") {
rCount++;
newStr += "R";
}
if (rCount == lCount) {
res.push(newStr);
rCount = 0;
lCount = 0;
newStr = "";
}
}
return res.length;
};