Why is "dir\n file.txt" 16?


  • -2
    D

    Shouldn't it be 12? Spaces can't be part of a filename.

    Additionally, I am not sure what is wrong with my code. I keep failing on this test case.

    def lengthLongestPath(self, input):
            """
            :type input: str
            :rtype: int
            """
            curr_path = []
            max_path = ""
            curr_level = 0
            max_len = 0
            sl = input.split("\n")
            for e in sl:
                e = e.strip(" ")
                if '.' in e:
                    curr_len = sum([len(i) for i in curr_path]) + len(curr_path) + len(e.lstrip("\t"))
                    if curr_len > max_len: 
                        max_len = curr_len
                else:
                    indent = len(e) - len(e.lstrip("\t"))
                    if indent > curr_level:
                        curr_level = indent
                        curr_path.append(e.lstrip("\t"))
                    elif indent < curr_level:
                        for _ in range(curr_level - indent - 1): 
                            if curr_path: curr_path.pop()
                    else:
                        if curr_path:
                            curr_path.pop()
                        curr_path.append(e.lstrip("\t"))
            return max_len
    

  • 0
    S

    "dir\n file.txt's max length is 9, if the character after \n is a space rather than a \t. It's filesystem structure is:

    dir
     file.txt
    

    where both folder "dir" and file " file.txt" are in the root directory. the length of " file.txt" is 9.



    "dir\n\tfile.txt"'s max length is 12. The filesystem's structure is

    dir
    	file.txt
    

    The length of "dir/file.txt" is 12.


Log in to reply
 

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