7 lines simple Python using stack


  • 0
    class Solution(object):
        def simplifyPath(self, path):
            """
            :type path: str
            :rtype: str
            """
            stack = []
            for x in path.split("/"):
                if x == "..":
                    if stack: stack.pop()
                elif x and x != ".":
                    stack.append(x)
            return "/" + "/".join(stack)
    

Log in to reply
 

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