Longest Absolute File Path

Suppose we have a file system that stores both files and directories. In text form, the file system is represented by a string where:

  • \n separates files and directories on different lines.
  • \t indicates the depth level of a file or directory.
  • Every file and directory has a unique absolute path, formed by joining the directory names needed to reach it with / separators.
  • Each directory name consists of letters, digits, and/or spaces.
  • Each file name is of the form name.extension, where name and extension consist of letters, digits, and/or spaces.

Given a string input representing the file system in this format, return the length of the longest absolute path to a file in the abstracted file system. If there is no file in the system, return 0.

Note that the test cases are generated such that the file system is valid and no file or directory name has length 0.

Example 1
Inputinput = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
Output20
We have only one file, and the absolute path is "dir/subdir2/file.ext" of length 20.
Example 2
Inputinput = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
Output32
The longest file path is "dir/subdir2/subsubdir2/file2.ext" of length 32.

Constraints

  • 1 <= input.length <= 10^4
  • input may contain lowercase or uppercase English letters, a new line character '\n', a tab character '\t', a dot '.', a space ' ', and digits.
  • All file and directory names have positive length.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate