MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / Solution

Class Solution

Python/split-a-string-in-balanced-strings.py:1–25  ·  view source on GitHub ↗

Time Complexity: O(N) Space Complexity: O(1)

Source from the content-addressed store, hash-verified

1class Solution:
2 """
3 Time Complexity: O(N)
4 Space Complexity: O(1)
5 """
6
7 def balanced_string_split(self, s: str) -> int:
8 # initialize variables
9 L_count, R_count = 0, 0
10 balanced_substring_count = 0
11
12 # parse the string
13 for char in s:
14
15 # update the number of Ls and the number of Rs so far
16 if char == 'L':
17 L_count += 1
18 elif char == 'R':
19 R_count += 1
20
21 # if the string is balanced, increment the balanced substrings count and reset the counters
22 if L_count == R_count:
23 balanced_substring_count += 1
24 L_count, R_count = 0, 0
25 return balanced_substring_count

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected