Skip to content

Instantly share code, notes, and snippets.

@shaselton
Created April 23, 2016 22:48
Show Gist options
  • Save shaselton/36d18eb9f214e1aec6790dbcdd8a80f4 to your computer and use it in GitHub Desktop.
Save shaselton/36d18eb9f214e1aec6790dbcdd8a80f4 to your computer and use it in GitHub Desktop.
def validBraces(string)
opening_character = ['(', '[', '{']
closing_character = [')', ']', '}']
string.split('').each_with_object([]) do |character, stack|
if opening_character.include?(character)
stack << character
else
return false if closing_character.index(character) != opening_character.index(stack.pop)
end
end
true
end
puts validBraces( "(){}[]" )
puts validBraces( "(}" )
puts validBraces( "[(])" )
puts validBraces( "([{}])" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment