388. Longest Absolute File Path Medium
1/**
2 * [388] Longest Absolute File Path
3 *
4 * Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture:
5 * <img alt="" src="https://assets.leetcode.com/uploads/2020/08/28/mdir.jpg" style="width: 681px; height: 322px;" />
6 * Here, we have dir as the only directory in the root. dir contains two subdirectories, subdir1 and subdir2. subdir1 contains a file file1.ext and subdirectory subsubdir1. subdir2 contains a subdirectory subsubdir2, which contains a file file2.ext.
7 * In text form, it looks like this (with ⟶ representing the tab character):
8 *
9 * dir
10 * ⟶ subdir1
11 * ⟶ ⟶ file1.ext
12 * ⟶ ⟶ subsubdir1
13 * ⟶ subdir2
14 * ⟶ ⟶ subsubdir2
15 * ⟶ ⟶ ⟶ file2.ext
16 *
17 * If we were to write this representation in code, it will look like this: "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext". Note that the '\n' and '\t' are the new-line and tab characters.
18 * Every file and directory has a unique absolute path in the file system, which is the order of directories that must be opened to reach the file/directory itself, all concatenated by '/'s. Using the above example, the absolute path to file2.ext is "dir/subdir2/subsubdir2/file2.ext". 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.
19 * Given a string input representing the file system in the explained 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.
20 * Note that the testcases are generated such that the file system is valid and no file or directory name has length 0.
21 *
22 * Example 1:
23 * <img alt="" src="https://assets.leetcode.com/uploads/2020/08/28/dir1.jpg" style="width: 401px; height: 202px;" />
24 * Input: input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
25 * Output: 20
26 * Explanation: We have only one file, and the absolute path is "dir/subdir2/file.ext" of length 20.
27 *
28 * Example 2:
29 * <img alt="" src="https://assets.leetcode.com/uploads/2020/08/28/dir2.jpg" style="width: 641px; height: 322px;" />
30 * Input: input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
31 * Output: 32
32 * Explanation: We have two files:
33 * "dir/subdir1/file1.ext" of length 21
34 * "dir/subdir2/subsubdir2/file2.ext" of length 32.
35 * We return 32 since it is the longest absolute path to a file.
36 *
37 * Example 3:
38 *
39 * Input: input = "a"
40 * Output: 0
41 * Explanation: We do not have any files, just a single directory named "a".
42 *
43 *
44 * Constraints:
45 *
46 * 1 <= input.length <= 10^4
47 * input may contain lowercase or uppercase English letters, a new line character '\n', a tab character '\t', a dot '.', a space ' ', and digits.
48 * All file and directory names have positive length.
49 *
50 */
51pub struct Solution {}
52
53// problem: https://leetcode.com/problems/longest-absolute-file-path/
54// discuss: https://leetcode.com/problems/longest-absolute-file-path/discuss/?currentPage=1&orderBy=most_votes&query=
55
56// submission codes start here
57
58impl Solution {
59 pub fn length_longest_path(input: String) -> i32 {
60 0
61 }
62}
63
64// submission codes end
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_388() {
72 }
73}
74
Back
© 2025 bowen.ge All Rights Reserved.