class Check

Public Class Methods

valid_parentheses(string) click to toggle source
# File lib/check_parans.rb, line 4
def self.valid_parentheses(string)
    only_parants = string.scan(/([(]|[)])/)
    if only_parants.empty?
      true
    elsif only_parants.length == 1
      false
    end
    k = false
    until k == true
      dummy = only_parants.length
      (0..only_parants.length - 1).each do |i|
        next unless only_parants[i] == ['('] && only_parants[i + 1] == [')']
        only_parants.delete_at(i)
        only_parants.delete_at(i)
        break
      end
      k = true if dummy == only_parants.length
    end
    only_parants.empty? ? true : false
end