
g0001_0100.s0020_valid_parentheses.solution.rb Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
# #Easy #Top_100_Liked_Questions #Top_Interview_Questions #String #Stack
# #Data_Structure_I_Day_9_Stack_Queue #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
# #2023_11_17_Time_53_ms_(90.52%)_Space_211.2_MB_(35.11%)
# @param {String} s
# @return {Boolean}
def is_valid(s)
stack = []
s.each_char do |c|
case c
when '(', '[', '{'
stack.push(c)
when ')'
return false if stack.empty? || stack.pop != '('
when '}'
return false if stack.empty? || stack.pop != '{'
when ']'
return false if stack.empty? || stack.pop != '['
else
return false
end
end
stack.empty?
end
© 2015 - 2025 Weber Informatics LLC | Privacy Policy