ruby - bracket-push

This commit is contained in:
2018-11-12 20:10:43 -05:00
parent 166d394ed3
commit 4c861d0858
3 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1,13 @@
# Brackets class
class Brackets
PAIRS = { '[' => ']', '{' => '}', '(' => ')' }.freeze
def self.paired?(text)
stack = []
text.chars.each do |char|
stack.push(char) if PAIRS.key?(char)
return false if PAIRS.key(char) && PAIRS.key(char) != stack.pop
end
stack.empty?
end
end