Balanced strings are those that have an equal quantity of 'L' and 'R' characters.

Given a balanced string s, split it into some number of substrings such that:


	Each substring is balanced.


Return the maximum number of balanced strings you can obtain.

&nbsp;
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'.


Example 2:

Input: s = "RLRRRLLRLL"
Output: 2
Explanation: s can be split into "RL", "RRRLLRLL", each substring contains same number of 'L' and 'R'.
Note that s cannot be split into "RL", "RR", "RL", "LR", "LL", because the 2nd and 5th substrings are not balanced.

Example 3:

Input: s = "LLLLRRRR"
Output: 1
Explanation: s can be split into "LLLLRRRR".


&nbsp;
Constraints:


	2 &lt;= s.length &lt;= 1000
	s[i] is either 'L' or 'R'.
	s is a balanced string.


class Solution:
    def balancedStringSplit(self, s: str) -> int:
        r_count=l_count=t_count=0
        for i in s:
            if i=='R':
                r_count+=1
            elif i=='L':
                l_count+=1
            if r_count==l_count:
                t_count+=1
                r_count=0
                l_count=0
            continue
        return t_count
class Solution {
    public int balancedStringSplit(String s) {
        int nl = 0;
        int nr = 0;
        int count = 0;
        for (int i = 0; i < s.length(); ++i) {
            if (s.substring(i,i+1).equals("L")) ++nl;
            else ++nr;
            if (nr == nl) {
                ++count;
            }
        }
        return count;
    }
}
class Solution {
public:
	int balancedStringSplit(string s) {

		int left = 0;
		int right = 0;
		int cnt = 0;

		for(int i=0 ; i<s.size() ; i++){
			if(s[i] == 'L'){
				left++;
			}
			if(s[i] == 'R'){
				right++;
			}

			if(left - right == 0){
				cnt++;
			}
		}

		return cnt;
	}
}
var balancedStringSplit = function(s) {
    let r_count = 0;
    let l_count = 0;
    let ans =0;
    for(let i = 0 ; i < s.length;i++){
        if(s[i]==='R') r_count++;
        else l_count++;

        if(l_count==r_count) {
         l_count=0
        r_count=0;
            ans++
        }

    }
    return ans
};
Split a String in Balanced Strings