A Python solution using stack


  • 0
    G
    class Solution:
        # @param path, a string
        # @return a string
        def simplifyPath(self, path):
            path = filter(lambda x: x, path.split('/'))
            output = []
    
            for section in path:
                if section == '.':
                    continue
                elif section == '..':
                    if output:
                        output.pop()
                else:
                    output.append(section)
            
            return '/' + '/'.join(output)

Log in to reply
 

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