Time Complexity: O(N) Space Complexity: O(1)
| 1 | class 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 |
nothing calls this directly
no outgoing calls
no test coverage detected