This question asks to take a unix like path and then shorten it (if possible). . refers to the current directory, and .. refers to the parent directory (if it exists). . can always be eliminated, and .. can eliminate a previous dir.

We can apply that algorithm. We split out any consecutive / by filtering out the input, and then create a stack of the file paths.

If we see a ’.’, skip it. If there’s a ’..’, then pop the stack if it exists, otherwise, append it as it is a dir name.

class Solution:
    def simplifyPath(self, path: str) -> str:
        split_path = filter(lambda x: x, path.split('/'))
 
        stack = []
 
        for part in split_path:
            if part == ".":
                continue
            if part == "..":
                if stack:
                    stack.pop()
                continue
            stack.append(part)
 
        return "/" + "/".join(stack)