Longest Absolute File Path
Real-world version: The Longest Route Down the CatalogueThe 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.
def length_longest_path(listing: str) -> int:public int lengthLongestPath(String listing)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's