Simple Python Stack based solution


  • 0
    S
        def lengthLongestPath(self, input):
            """
            :type input: str
            :rtype: int
            """
            level, maxLength, current_len = 0, 0, 0
            current = []
            for line in input.split("\n"):
                stripped = line.lstrip("\t")
                tabs = len(line) - len(stripped)
    
                if tabs <= level and current:
                    for i in range(0, level - tabs + 1):
                        x = current.pop()
                        current_len -= x
    
                current.append(len(stripped))
                current_len += current[-1]
                level = tabs
                
                if "." in line: # This is a file!
                    maxLength = max(current_len + len(current) - 1, maxLength)
            
            return maxLength
    

Log in to reply
 

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