Leetcode 1341 Solution

This article provides solution to leetcode question 1341 (split-a-string-in-balanced-strings)

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

Solution

class Solution:
    def balancedStringSplit(self, s: str) -> int:
        curr_sum = 0

        ans = 0
        for ch in s:
            if ch == 'L':
                curr_sum += 1
            elif ch == 'R':
                curr_sum -= 1

            if curr_sum == 0:
                ans += 1

        return ans