- Time complexity:
- Space complexity:
VALID_CHARS = {
'(' => ')',
'{' => '}',
'[' => ']',
}
# @param {String} s
# @return {Boolean}
def is_valid(s)
chars = s.split('')
return false if chars.size < 2
openTags = []
result = chars.all? do |c|
if VALID_CHARS.keys.include?(c)
openTags << c
true
else
res = VALID_CHARS[openTags.last] == c
openTags.pop if res
res
end
end
result && openTags.empty?
end