As easy as can be, in Ruby


  • 0
    Y
    def simplify_path(path)
      stack = []
      path.split("/").each do |part|
        case part
        when "", "." # do nothing
        when ".."
          stack.pop if stack.any?
        else
          stack.push part
        end
      end
      
      "/" + stack.join("/")
    end
    

Log in to reply
 

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