Simplify Path

Problem: Simplify Path

We use a stack to maintain folders and if we meet '..' we pop a folder from the stack.

Code in Python:

class Solution(object):
    def simplifyPath(self, path):
        """
        :type path: str
        :rtype: str
        """
        _path = path.split("/")
        stack = []
        for s in _path:
            if s == ".." and stack: stack.pop()
            elif s == ".." or s == "." or s == "": continue
            else: stack.append(s)
        if not stack: return "/"
        res = ""
        for s in stack:
            res += "/" + s
        return res

results matching ""

    No results matching ""