Published:
Updated:

https://leetcode.com/problems/split-a-string-in-balanced-strings/


Solution

class Solution {
    static int balancedStringSplit(String s) {
        int answer = 0;
        int cntL = 0;
        int cntR = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'R') {
                cntR++;
            } else { // s.charAt(i) == 'L'
                cntL++;
            }

            if (cntL != 0 || cntR != 0) {
                if (cntR == cntL) {
                    answer++;
                }
            }
        }

        return answer;
    }

    public static void main(String[] args) {
        System.out.println(balancedStringSplit("RLRRLLRLRL"));
        System.out.println(balancedStringSplit("RLRRRLLRLL"));
        System.out.print(balancedStringSplit("LLLLRRRR"));
    }
}

Leave a comment