dyoder (owner)

Revisions

gist: 201655 Download_button fork
public
Public Clone URL: git://gist.github.com/201655.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
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