Would like some feedback on refactoring my ruby solution


  • 0
    R

    I'm new to ruby. I know that this solution is not the most efficient out there but I was curious how could I refactor my current solution to make it as rubyist as possible (if it's not that way already) :p thanks!

    # @param {String} s
    # @return {Boolean}
    def is_valid(s)
        return false if s.size.odd?
        
        paren_stack = []
        open_parens = ['{', '(', '[']
        
        s.split("").each do |char|
            if open_parens.include?(char) 
                paren_stack.push(char)
            else
                most_recent_open_paren = paren_stack.pop
                return false if most_recent_open_paren != get_open_paren(char)
            end
        end
        paren_stack.size > 0 ? false : true
    end
    
    private 
    
    def get_open_paren(closing_paren)
        matches = { ']': '[', ')': '(', '}': '{' }
        matches[closing_paren.to_sym]
    end
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.