simple python solution, 32ms


  • 0
    M
    class Solution(object):
        def lengthLongestPath(self, input):
            """
            :type input: str
            :rtype :int
            """
            depth_level = 0
            abs_path = list()
            result = 0
    
            for p in input.split("\n"):
                if "\t" * depth_level in p:
                    abs_path.append(p.replace("\t", ""))
                    depth_level += 1
                else:
                    if "." not in abs_path[-1]: 
                        abs_length = 0
                    else:
                        abs_length = len('/'.join(abs_path))
                    new_level = p.count("\t")               
                    abs_path = abs_path[:new_level]
                    abs_path.append(p.replace("\t", ""))
                    result = max(result, abs_length)
    
                    depth_level = new_level + 1
    
            abs_length = len('/'.join(abs_path))
            if "." not in abs_path[-1]:  
                abs_length = 0
            return max(result, abs_length)
    

Log in to reply
 

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