Skip to content
LC-0071 Medium LeetCode

71. Simplify Path

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 47% Topics: String, Stack
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(n)

class Solution(object):
    # @param path, a string
    # @return a string
    def simplifyPath(self, path):
        stack, tokens = [], path.split("/")
        for token in tokens:
            if token == ".." and stack:
                stack.pop()
            elif token != ".." and token != "." and token:
                stack.append(token)
        return "/" + "/".join(stack)

Solution from kamyu104/LeetCode-Solutions · MIT