Write a method 'valid_string?' that accepts a string. It returns true if the brackets, parentheses, and curly braces close correctly. It returns false otherwise.
valid_string?("[ ]") # returns true
valid_string?("[ ") # returns false
valid_string?("[ ( text ) {} ]") # returns true
valid_string?("[ ( text { ) } ]") # returns false
My code: Is returning false for everything. Even tried using explicit booleans for individual cases {} || () ||, etc. Did not work. Either returns true or false for everything. Is it my driver code?
def valid_string?(str)
if str == ("\[\s+]")
true
else
false
end
end
UPDATED SOLUTION:------------------------------------------------ Yes! #match definitely worked out better! Although my last line of test code is evaluating to true. When it should be false. . .
def valid_string?(str)
if str.match "(\\[.+\\])" || "|(\\(\\))" || "|({})"
return true
else
return false
end
end
puts valid_string?("[ ]") # returns true
puts valid_string?("[ ") # returns false
puts valid_string?("[ ( text ) {} ]") # returns true
puts valid_string?("[ ( text { ) } ]") # returns false
An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type.
The valid parentheses problem involves checking that: all the parentheses are matched, i.e., every opening parenthesis has a corresponding closing parenthesis. the matched parentheses are in the correct order, i.e., an opening parenthesis should come before the closing parenthesis.
If a symbol is an opening parenthesis, push it on the stack as a signal that a corresponding closing symbol needs to appear later. If, on the other hand, a symbol is a closing parenthesis, pop the stack. As long as it is possible to pop the stack to match every closing symbol, the parentheses remain balanced.
Check for Balanced Bracket expression using Stack:Whenever you hit a closing bracket, search if the top of the stack is the opening bracket of the same nature. If this holds then pop the stack and continue the iteration, in the end if the stack is empty, it means all brackets are well-formed .
How about a simple counting routine?
def valid_string?(str)
match_count = 0
str.each_char do |c|
match_count += 1 if [ '[', '{', '(' ].include?(c)
match_count -= 1 if [ ']', '}', ')' ].include?(c)
end
return match_count == 0
end
I think it might be complicated to use regex
to solve this problem. Here is a potential solution: You may use a stack to record the left symbol like {
, [
, (
in the traverse. Each time you met the right symbol, just check whether the symbol on the stack top matches this right symbol. Simply return false
if not match.
Below is my code:
def valid_string?(str)
stack = []
symbols = { '{' => '}', '[' => ']', '(' => ')' }
str.each_char do |c|
stack << c if symbols.key?(c)
return false if symbols.key(c) && symbols.key(c) != stack.pop
end
stack.empty?
end
puts valid_string?('[ ]') # returns true
puts valid_string?('[ ') # returns false
puts valid_string?('[ ( text ) {} ]') # returns true
puts valid_string?('[ ( text { ) } ]') # returns false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With