Leetcode Problem 1221. Split a String in Balanced Strings

1221. Split a String in Balanced Strings

Leetcode Solutions

Greedy Counting Approach

  1. Initialize a counter res to 0 to store the number of balanced substrings found.
  2. Initialize a counter cnt to 0 to keep track of the balance between 'L' and 'R' characters.
  3. Iterate through each character c in the string s.
    • If c is 'L', increment cnt.
    • If c is 'R', decrement cnt.
  4. If cnt becomes 0, it means we have found a balanced substring, so increment res.
  5. Continue the iteration until the end of the string.
  6. Return the value of res.
UML Thumbnail

Stack-Based Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...