Skip to content

Instantly share code, notes, and snippets.

@bparanj
Created August 20, 2020 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bparanj/fab7bb76a50dedb4ef60a4fc0f56e537 to your computer and use it in GitHub Desktop.
Save bparanj/fab7bb76a50dedb4ef60a4fc0f56e537 to your computer and use it in GitHub Desktop.
# @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
@bparanj
Copy link
Author

bparanj commented Aug 20, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment