Problem statement

https://binarysearch.com/problems/Unix-Path-Resolution/

Solution

The same as Leetcode 0071. Simplify Path, just different format of I/O

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, path):
        stack = []
        for elem in path:
            if stack and elem == "..":
                stack.pop()
            elif elem not in [".", "", ".."]:
                stack.append(elem)
                
        return stack