Solving Popular Algorithms: Balancing Strings

A kitty trying to find balance
s = "RLRRLLRLRL"
balancedStringSplit = (s) => {
let balance = 0
let answer = 0
for (let char of s) {

if (char === "R") {
balance++
} else {
balance--
}
if (balance === 0) {
answer++
}
}
return answer
};
A monkey with perfect balance

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store