You are viewing a single comment's thread. Return to all comments →
Clean Ruby solution:
BRACKETS = { '(' => ')', '{' => '}', '[' => ']' } def is_balanced(expression) stack = [] expression.each_char do |c| if BRACKETS.key? c stack << c elsif BRACKETS[stack.pop] != c return false end end stack.empty? end gets.to_i.times do puts is_balanced(gets.chomp) ? 'YES' : 'NO' end
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Stacks: Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
Clean Ruby solution: