java.lang.Object
g0301_0400.s0388_longest_absolute_file_path.Solution

public class Solution extends Object
388 - Longest Absolute File Path.<p>Medium</p> <p>Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture:</p> <p><img src="https://assets.leetcode.com/uploads/2020/08/28/mdir.jpg" alt="" /></p> <p>Here, we have <code>dir</code> as the only directory in the root. <code>dir</code> contains two subdirectories, <code>subdir1</code> and <code>subdir2</code>. <code>subdir1</code> contains a file <code>file1.ext</code> and subdirectory <code>subsubdir1</code>. <code>subdir2</code> contains a subdirectory <code>subsubdir2</code>, which contains a file <code>file2.ext</code>.</p> <p>In text form, it looks like this (with \u27f6 representing the tab character):</p> <pre><code> dir \u27f6 subdir1 \u27f6 \u27f6 file1.ext \u27f6 \u27f6 subsubdir1 \u27f6 subdir2 \u27f6 \u27f6 subsubdir2 \u27f6 \u27f6 \u27f6 file2.ext </code></pre> <p>If we were to write this representation in code, it will look like this: <code>&quot;dir \tsubdir1 \t\tfile1.ext \t\tsubsubdir1 \tsubdir2 \t\tsubsubdir2 \t\t\tfile2.ext&quot;</code>. Note that the <code>' '</code> and <code>'\t'</code> are the new-line and tab characters.</p> <p>Every file and directory has a unique <strong>absolute path</strong> in the file system, which is the order of directories that must be opened to reach the file/directory itself, all concatenated by <code>'/'s</code>. Using the above example, the <strong>absolute path</strong> to <code>file2.ext</code> is <code>&quot;dir/subdir2/subsubdir2/file2.ext&quot;</code>. Each directory name consists of letters, digits, and/or spaces. Each file name is of the form <code>name.extension</code>, where <code>name</code> and <code>extension</code> consist of letters, digits, and/or spaces.</p> <p>Given a string <code>input</code> representing the file system in the explained format, return <em>the length of the <strong>longest absolute path</strong> to a <strong>file</strong> in the abstracted file system</em>. If there is no file in the system, return <code>0</code>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/08/28/dir1.jpg" alt="" /></p> <p><strong>Input:</strong> input = &ldquo;dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext&rdquo;</p> <p><strong>Output:</strong> 20</p> <p><strong>Explanation:</strong> We have only one file, and the absolute path is &ldquo;dir/subdir2/file.ext&rdquo; of length 20.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/08/28/dir2.jpg" alt="" /></p> <p><strong>Input:</strong> input = input = &ldquo;dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext&rdquo;</p> <p><strong>Output:</strong> 32</p> <p><strong>Explanation:</strong> We have two files: &ldquo;dir/subdir1/file1.ext&rdquo; of length 21 &ldquo;dir/subdir2/subsubdir2/file2.ext&rdquo; of length 32. We return 32 since it is the longest absolute path to a file.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> input = &ldquo;a&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> We do not have any files, just a single directory named &ldquo;a&rdquo;.</p> <p><strong>Example 4:</strong></p> <p><strong>Input:</strong> input = &ldquo;file1.txt<br /> file2.txt<br /> longfile.txt&rdquo;</p> <p><strong>Output:</strong> 12</p> <p><strong>Explanation:</strong> 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 &ldquo;longfile.txt&rdquo; with length 12.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= input.length <= 10<sup>4</sup></code></li> <li><code>input</code> may contain lowercase or uppercase English letters, a new line character <code>' '</code>, a tab character <code>'\t'</code>, a dot <code>'.'</code>, a space <code>' '</code>, and digits.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • lengthLongestPath

      public int lengthLongestPath(String input)