All problems
1132MediumStringStackDepth-First Search

The Longest Route Down the Catalogue

Tracked in this browser only
Write code

Trains the technique from

LeetCode 388Longest Absolute File Path

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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