Class: Check

Inherits:
Object
  • Object
show all
Defined in:
lib/check_parans.rb

Class Method Summary collapse

Class Method Details

.valid_parentheses(string) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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