Skip to content
LC-1221 Easy LeetCode

1221. Split a String in Balanced Strings

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 87% Topics: String, Greedy, Counting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def balancedStringSplit(self, s):
        """
        :type s: str
        :rtype: int
        """
        result, count = 0, 0      
        for c in s:
            count += 1 if c == 'L' else -1            
            if count == 0:
                result += 1
        return result

Solution from kamyu104/LeetCode-Solutions · MIT