Created
August 20, 2020 00:37
-
-
Save bparanj/fab7bb76a50dedb4ef60a4fc0f56e537 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @param {String} s | |
# @return {Integer} | |
def balanced_string_split(s) | |
counter = 0 | |
balance = 0 | |
characters = s.chars | |
characters.each do |c| | |
balance += 1 if c == 'L' | |
balance -= 1 if c == 'R' | |
counter += 1 if balance == 0 | |
end | |
counter | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hint 1: Loop from left to right maintaining a balance variable when it gets an L increase it by one otherwise decrease it by one.
Hint 2: Whenever the balance variable reaches zero then we increase the answer by one.