Last active
July 20, 2018 08:07
-
-
Save vedant1811/aa1b70dd31f6566d1e526ea154d34f45 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def balance(string) | |
offset = 0 | |
swaps = 0 | |
string.each_char do |c| | |
if c == '[' | |
if offset > 0 | |
swaps += offset | |
end | |
offset -= 1 | |
else | |
offset += 1 | |
end | |
end | |
swaps | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment