Created
April 23, 2016 22:48
-
-
Save shaselton/36d18eb9f214e1aec6790dbcdd8a80f4 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
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