Skip to content

Instantly share code, notes, and snippets.

@vedant1811
Last active July 20, 2018 08:07
Show Gist options
  • Save vedant1811/aa1b70dd31f6566d1e526ea154d34f45 to your computer and use it in GitHub Desktop.
Save vedant1811/aa1b70dd31f6566d1e526ea154d34f45 to your computer and use it in GitHub Desktop.
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