package leetcode;

/**
 * https://leetcode.com/problems/split-a-string-in-balanced-strings/
 */
public class Problem1221 {
    public int balancedStringSplit(String s) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1221 prob = new Problem1221();
        System.out.println(prob.balancedStringSplit("RLRRLLRLRL")); // 4
        System.out.println(prob.balancedStringSplit("RLLLLRRRLR")); // 3
        System.out.println(prob.balancedStringSplit("LLLLRRRR")); // 1
    }
}

package leetcode;

/**
 * https://leetcode.com/problems/split-a-string-in-balanced-strings/
 */
public class Problem1221 {
    public int balancedStringSplit(String s) {
        int answer = 0;
        int lCount = 0;
        int rCount = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'L') {
                lCount++;
            } else if (s.charAt(i) == 'R') {
                rCount++;
            }
            if (lCount == rCount) {
                answer++;
                lCount = 0;
                rCount = 0;
            }
        }
        return answer;
    }
}

Solve problem 1221