Created
October 4, 2009 21:50
-
-
Save dyoder/201655 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
DELIMITERS = [ ['(',')'], ['{','}'], ['[',']'] ] | |
def match?( string, stack = [] ) | |
return true if string.empty? and stack.empty? | |
first = string[0,1] ; DELIMITERS.any? do | open, close | | |
case first | |
when open then match?( string[1..-1], stack.clone.push( close ) ) | |
when close then match?( string[1..-1], stack ) if stack.clone.pop == close | |
else false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment