- Published on
Find the longest path file
- Authors
- Name
- Moch Lutfi
- @kaptenupi
Problem
Suppose we represent our file system as a string. For example, the string "user\n\tpictures\n\tdocuments\n\t\tnotes.txt"
represents:
_4user_4 pictures_4 documents_4 notes.txt
The directory user
contains an empty sub-directory pictures
and a sub-directory documents
containing a file notes.txt
.
The string "user\n\tpictures\n\t\tphoto.png\n\t\tcamera\n\tdocuments\n\t\tlectures\n\t\t\tnotes.txt"
represents:
_7user_7 pictures_7 photo.png_7 camera_7 documents_7 lectures_7 notes.txt
The directory user
contains two sub-directories pictures
and documents
. pictures
contains a file photo.png
and an empty second-level sub-directory camera
. documents
contains a second-level sub-directory lectures
containing a file notes.txt
.
We want to find the longest (as determined by the number of characters) absolute path to a file within our system. For example, in the second example above, the longest absolute path is "user/documents/lectures/notes.txt"
, and its length is 33
(not including the double quotes).
Given a string 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 not a file in the file system, return 0
.
Notes:
- Due to system limitations, test cases use form feeds (
'\f'
, ASCII code12
) instead of newline characters. - File names do not contain spaces at the beginning.
Example
For fileSystem = "user\f\tpictures\f\tdocuments\f\t\tnotes.txt"
, the output should besolution(fileSystem) = 24
.
The longest path is "user/documents/notes.txt"
, and it consists of 24
characters.
Constraints
File system in the format described above. It is guaranteed that:
- the name of a file contains at least a
.
and an extension; - the name of a directory or sub-directory does not contain a
.
.
Note: Due to system limitations, newline characters are given as form feeds ('\f'
, ASCII code 12
) in our test cases.
Guaranteed constraints:1 ≤ fileSystem.length ≤ 6310
.
Solution
_24import "strings"_24_24func solution(fileSystem string) int {_24 lines := strings.Split(fileSystem, "\f")_24 stack := []string{}_24 maxLen := 0_24 for _, line := range lines {_24 tabs := strings.Count(line, "\t")_24 // trim \t to get name_24 name := strings.TrimLeft(line, "\t")_24 for len(stack) > tabs {_24 stack = stack[:len(stack)-1] // pop stack_24 }_24 if strings.Contains(name, ".") {_24 currLen := len(strings.Join(append(stack, name), "/"))_24 if currLen > maxLen {_24 maxLen = currLen_24 }_24 } else {_24 stack = append(stack, name)_24 }_24 }_24 return maxLen_24}