Skip to content

Instantly share code, notes, and snippets.

@mirjoy
Created May 26, 2015 22:01
Show Gist options
  • Save mirjoy/47784b715be4c06e7ce9 to your computer and use it in GitHub Desktop.
Save mirjoy/47784b715be4c06e7ce9 to your computer and use it in GitHub Desktop.
well formed string
class StringForm
def valid_string?(str)
string_array = str.chars
new_array = []
string_array.length.times do
if ["[", "{", "("].include?(string_array.first)
new_array << string_array.shift
elsif ["]", "}", ")"].index(string_array.first) == ["[", "{", "("].index(new_array.last)
string_array.shift && new_array.pop
else
new_array << string_array.shift
end
end
new_array.empty?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment