Skip to content

Instantly share code, notes, and snippets.

@Nitesh-Mishra
Created June 9, 2018 21:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nitesh-Mishra/b80d72aa45c9b0a6ee0e7922d1041fee to your computer and use it in GitHub Desktop.
Save Nitesh-Mishra/b80d72aa45c9b0a6ee0e7922d1041fee to your computer and use it in GitHub Desktop.
check balanced parentheses in ruby
# check balanced parentheses in ruby :
#
# $ ruby check_balanced_parentheses.rb
# Enter string :
# ((()))
# true
def check_balanced_parentheses?(entered_string)
stack = []
entered_string.chars.each do |token|
case token
when '('
stack.push '('
when ')'
if stack.empty? || stack.pop != '('
return false
end
end
end
stack.empty?
end
puts "Enter string :"
entered_string = gets
puts check_balanced_parentheses?(entered_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment