Created
May 13, 2019 12:34
-
-
Save deepakkumarnd/a5042179d4d0c9c8ac1046bc76606394 to your computer and use it in GitHub Desktop.
Revisions
-
deepakkumarnd created this gist
May 13, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ TOKENS = { '}' => '{', ')' => '(', ']' => '[' }.freeze def valid_brace?(input) stack = [] valid = true input.each_char do |c| case c when '(', '[', '{' then stack.push(c) when ')', ']', '}' then valid = (stack.pop == TOKENS[c]) else valid = false end break unless valid end valid && stack.empty? end inputs = ['()[]{}', '([{}])', '[({})](]', '[(])', '(}'] inputs.each do |input| text = valid_brace?(input) ? "valid" : "invalid" puts "#{input} is #{text}" end