Skip to content

Instantly share code, notes, and snippets.

@dyoder
Created October 4, 2009 21:50
Show Gist options
  • Save dyoder/201655 to your computer and use it in GitHub Desktop.
Save dyoder/201655 to your computer and use it in GitHub Desktop.
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