Python string management with stack


  • 0
    class Solution(object):
        def simplifyPath(self, path):
            operations = path.split('/')
            stack = []
            for operation in operations:
                if operation != '.' and operation != '':
                    if operation == '..':
                        if stack:
                            stack.pop()
                    else:
                        stack.append(operation)
            return '/' + '/'.join(stack)
    

Log in to reply
 

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