def lengthlongestpathinput maxlen 0 pathlen 0 0 for line in input splitlines print printline line name line strip t printname name depth lenline lenname printdepth depth if in name maxlen maxmaxlen pathlendepth lenname else pathlendepth 1 pathlendepth lenname 1 printmaxlen maxlen return maxlen def lengthlongestpathinput paths input splitn level 0 10 maxlength 0 for path in paths print levelidx path rfindt printpath path printpath rfindt path rfindt printlevelidx levelidx printlevel level levellevelidx 1 levellevelidx lenpath levelidx 1 printlevel level if in path maxlength maxmaxlength levellevelidx1 1 printmaxlen maxlength return maxlength type input str rtype int def lengthlongestpath input maxlen 0 pathlen 0 0 for line in input splitlines print print line line name line strip t print name name depth len line len name print depth depth if in name maxlen max maxlen pathlen depth len name else pathlen depth 1 pathlen depth len name 1 print maxlen maxlen return maxlen def lengthlongestpath input paths input split n level 0 10 maxlength 0 for path in paths print levelidx path rfind t print path path print path rfind t path rfind t print levelidx levelidx print level level level levelidx 1 level levelidx len path levelidx 1 print level level if in path maxlength max maxlength level levelidx 1 1 print maxlen maxlength return maxlength type input str rtype int running length and max length keep track of the name length the depth of current dir or file go back to the correct depth 1 is the length of increase current length update maxlen only when it is a file 1 is to minus one
def length_longest_path(input):
    
    curr_len, max_len = 0, 0    
    stack = []    
    for s in input.split('\n'):
        print("---------")
        print("<path>:", s)
        depth = s.count('\t')    
        print("depth: ", depth)
        print("stack: ", stack)
        print("curlen: ", curr_len)
        while len(stack) > depth:    
            curr_len -= stack.pop()
        stack.append(len(s.strip('\t'))+1)   
        curr_len += stack[-1]    
        print("stack: ", stack)
        print("curlen: ", curr_len)
        if '.' in s:    
            max_len = max(max_len, curr_len-1)    
    return max_len


st = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdirectory1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
st2 = "a\n\tb1\n\t\tf1.txt\n\taaaaa\n\t\tf2.txt"
print("path:", st2)

print("answer:", length_longest_path(st2))