Show problems as
Classic problem #388MediumStringStackDepth-First Search

Longest Absolute File Path

Real-world version: The Longest Route Down the Catalogue
Tracked in this browser only
Read Longest Absolute File Path on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A catalogue arrives as one string listing. Entries are separated by the newline character, and each entry is preceded by as many tab characters as its depth, so an entry at depth 0 has none, one at depth 1 has one, and so on. An entry at depth d + 1 sits inside the nearest earlier entry at depth d.

An entry whose name holds a dot is a crate, and every other entry is a shelf.

The route to an entry is the names from the outermost shelf down to the entry itself, joined by single / characters. Return the length of the longest route to a crate, or 0 when the catalogue holds no crate at all.

Examples

Example 1

Input
listing = "dir\n\tsub\n\tfile.a\n\tsub2\n\t\tfile.bb"
Output
16

The deeper crate's route joins three names with two slashes for sixteen characters, while the crate one level in gives only ten.

Example 2

Input
listing = "shelf"
Output
0

The one entry holds no dot, so it is a shelf and the catalogue holds no crate at all.

Example 3

Input
listing = "a.b"
Output
3

The single entry holds a dot, so it is a crate and its route is just its own name.

Constraints

  • 1 <= listing.length <= 10^4
  • listing may hold English letters, newline characters, tab characters, dots, spaces and digits
  • every name holds at least one character

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def length_longest_path(listing: str) -> int:
Java
public int lengthLongestPath(String listing)