52ms Simple Python Solution Using Stack


  • 0
    C

    A simple Python solution using stack.
    It's really easy to understand and no need for explanation.

    def simplifyPath(self, path):
        folders = []
        for c in path.split('/'):
            if not c:
                continue
            elif c == '.':
                continue
            elif c == '..':
                if len(folders) != 0:
                    folders.pop()
            else:
                folders.append(c)
                
        return '/' + '/'.join(folders)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.