Tuesday, February 16, 2021

Leetcode Everyday: 1221. Split a String in Balanced Strings. Easy

public class Solution {
    public int BalancedStringSplit(string s) {
        var balanceCount = 0;
        var balanceDetector = 0;
        foreach (var c in s) {
            balanceDetector += c == 'L' ? 1 : -1;
            balanceCount += balanceDetector == 0 ? 1 : 0;
        }
        return balanceCount;
    }
}

No comments:

Post a Comment