71. Simplify Path - cocoder39/coco39_LC GitHub Wiki
come up with a stack solution is not that challenging. However, come with up all test cases and delivery code to address all of them is a challenge
class Solution:
def simplifyPath(self, path: str) -> str:
tokens = path.split('/')
stack = []
for token in tokens:
if token == '' or token == '.': # indicating //
continue
elif token == '..':
if stack:
stack.pop()
else:
stack.append(token)
return '/' + '/'.join(stack) # edge case: stack is empty