Ruby 7 lines


  • 0
    L
    def simplify_path(path)
        paths = path.split("/")
        stack = []
        paths.each do |p|
            stack.pop if !stack.empty? && p == ".."
            stack.push(p) if p != ".." && p != "." && !p.empty?
        end
        return "/" + stack.map.with_index{|s, i| s = s + (i < stack.length - 1 ? "/" : "")}.join
    end
    

Log in to reply
 

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