[LeetCode] 388. Longest Absolute File Path The longest absolute path of the file (Medium) (JAVA)

[LeetCode] 388. Longest Absolute File Path The longest absolute path of the file (Medium) (JAVA)

Subject address: https://leetcode.com/problems/longest-absolute-file-path/

Title description:

Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture:

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.

In text form, it looks like this (with ⟶ representing the tab character):

dir
⟶ subdir1
⟶ ⟶ file1.ext
⟶ ⟶ subsubdir1
⟶ subdir2
⟶ ⟶ subsubdir2
⟶ ⟶ ⟶ file2.ext

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.

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.

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.

Example 1:

Input: input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
Output: 20
Explanation: We have only one file, and the absolute path is "dir/subdir2/file.ext" of length 20.

Example 2:

Input: input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
Output: 32
Explanation: We have two files:
"dir/subdir1/file1.ext" of length 21
"dir/subdir2/subsubdir2/file2.ext" of length 32.
We return 32 since it is the longest absolute path to a file.

Example 3:

Input: input = "a"
Output: 0
Explanation: We do not have any files, just a single directory named "a".

Example 4:

Input: input = "file1.txt\nfile2.txt\nlongfile.txt"
Output: 12
Explanation: There are 3 files at the root directory.
Since the absolute path for anything at the root directory is just the name itself, the answer is "longfile.txt" with length 12.

Constraints:

  • 1 <= input.length <= 10^4
  • input may contain lowercase or uppercase English letters, a new line character ‘\n’, a tab character ‘\t’, a dot ‘.’, a space ’ ', and digits.

General idea

Assume that the file system is as shown in the figure below:

Here dir is used as the only directory in the root directory. dir contains two subdirectories subdir1 and subdir2. subdir1 contains the file file1.ext and the sub-directory subsubdir1; subdir2 contains the sub-directory subsubdir2, which contains the file file2.ext.

In the text format, it is as follows (⟶ represents a tab character):

dir
⟶ subdir1
⟶ ⟶ file1.ext
⟶ ⟶ subsubdir1
⟶ subdir2
⟶ ⟶ subsubdir2
⟶ ⟶ ⟶ file2.ext
If it is a code representation, the above file system can be written as "dir\n\tsubdir1\n\t\tfile1. \t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext". '\n' and'\t' are line breaks and tabs, respectively.

Each file and folder in the file system has a unique absolute path, that is, the order of directories that must be opened to reach the location of the file/directory, and all paths are connected by'/'. In the above example, the absolute path to file2.ext is "dir/subdir2/subsubdir2/file2.ext". Each directory name consists of letters, numbers and/or spaces, and each file name follows the format of name.extension, where the name and extension consist of letters, numbers and/or spaces.

Given a string input representing the file system in the above format, return the length of the longest absolute path to the file in the file system. If there is no file in the system, 0 is returned.

Problem-solving method

  1. First divide all characters according to the newline character "\n"
  2. According to the number of tabs "\t", you can know the level of the current directory
  3. The length of the next level is equal to the length of the previous level + the length of the current level
  4. Encounter a place that contains ".", it means that it is a file, otherwise it is a directory (because the directory has a "dir/subdir2"), and the length must be +1 each time
class Solution {
    public int lengthLongestPath(String input) {
        int max = 0;
        String[] strs = input.split("\n");
        int[] lens = new int[input.length() + 1];
        for (String str: strs) {
            int level = str.lastIndexOf("\t") + 2;
            int len = str.length() - level + 1;
            if (str.contains(".")) {
                max = Math.max(max, lens[level - 1] + len);
            } else {
                lens[level] = lens[level - 1] + len + 1;
            }
        }
        return max;
    }
}

Execution time: 1 ms, beating 87.80% of Java users
Memory consumption: 36.7 MB, beating 26.28% of Java users

Welcome to pay attention to my official account, LeetCode updates one question every day

Guess you like

Origin blog.csdn.net/qq_16927853/article/details/111879961