def mBrackets str, brackets = {'(' => ')', '[' => ']', '{' => '}', '<' => '>'}
matched = []
result = [["", 0]]
valid = [0]
str.clone.each_char do |c|
valid.each{|v| result[v][0].concat(c)}
if brackets.keys.include?(c)
matched.push(c)
valid.push(result.size)
result.push(["", valid.size - 1])
elsif brackets.values.include?(c)
if brackets[matched[-1]] == c
result[valid[-1]][0].chop!
valid.pop
matched.pop
else
raise ArgumentError, "False Matching!"
end
end
end
result
end
def mBrackets str, brackets = {'(' => ')', '[' => ']', '{' => '}', '<' => '>'}
matched = []
result = [["", 0]]
valid = [0]
str.clone.each_char do |c|
valid.each{|v| result[v][0].concat(c)}
if brackets.keys.include?(c)
matched.push(c)
valid.push(result.size)
result.push(["", valid.size - 1])
elsif brackets.values.include?(c)
if brackets[matched[-1]] == c
result[valid[-1]][0].chop!
valid.pop
matched.pop
else
raise ArgumentError, "False Matching!"
end
end
end
result
end