Ugliest Python solution to make you feel better.


  • 0
    Q
    class Solution(object):
        def lengthLongestPath(self, input):
          
            if not input: return 0
            stack = []
            maxLength = 0
            length = 0
            i = 0
            current = 0
            nextLevel = 0
            print input
            while i < len(input):
                current = nextLevel
                file = self.getFirstWord(input[i:])
                i += (len(file) + 1)
                if "." in file:
                    length += (len(file))
                    maxLength = max(maxLength, length)
                else:
                    length += (len(file) + 1)
                stack.append(file)
                nextLevel = self.getNextLevel(input[i:])
                i += (nextLevel)
                if nextLevel <= current and nextLevel >= 0:
                    for x in range(current - nextLevel + 1):
                        
                        word = stack.pop()
                        if "." in word:
                            length -= (len(word))
                        else:
                            length -= (len(word) + 1)
            return maxLength
                
        
        def getFirstWord(self, input):
            word = ""
            i = 0
            while i < len(input) and input[i] !="\n":
                word += input[i]
                i += 1
            return word
        
        def getNextLevel(self, input):
            level = 0
            for i in range(len(input)):
                if input[i] == "\t":
                    level += 1
                if input[i] != "\n" and input[i] != "\t":
                    return level
        
            return level

Log in to reply
 

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