[LintCode] System Longest File Path
Suppose we abstract our file system by a string in the following manner:
The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
represents:
dir
subdir1
subdir2
file.ext
The directory dir
contains an empty sub-directory subdir1
and a sub-directory subdir2
containing a file file.ext.
The string
"dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
represents:
dir
subdir1
file1.ext
subsubdir1
subdir2
subsubdir2
file2.ext
The directory dir
contains two sub-directories subdir1
and subdir2
. subdir1
contains a file file1.ext and an empty second-level sub-directory subsubdir1
. subdir2
contains a second-level sub-directory subsubdir2
containing a file file2.ext
.
We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is "dir/subdir2/subsubdir2/file2.ext"
, and its length is 32
(not including the double quotes).
Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return 0
.
Notice
- The name of a file contains at least a
.
and an extension. - The name of a directory or sub-directory will not contain a
.
- Time complexity required:
O(n)
where n is the size of the input string. - Notice that
a/aa/aaa/file1.txt
is not the longest file path, if there is another pathaaaaaaaaaaaaaaaaaaaaa/sth.png
.
This problem is a straightforward string simulation algorithm problem.
path[i] stores the prefix dir length for level i. path[0] is 0 since level 0 does not have any prefix;
for example string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext", path[1] is 4(representing dir/ as level 1's prefix).
1. split the input string around "\n" to get all dirs' and files' relative paths to their current level.
2. replace all "\t" with empty character "" for each substring and calcuate its level by checking how much shorter
the new substring is after the replace operation.
3. if the substring is a file, then add its prefix dir length and update the max absolute file path length if needed.
if not a file, update the prefix dir length for the next level.
4. each time the logic goes back to a higher level, it overwrites the stale prefix length values.
For example, "dir\n\tsubdir1\n\tsubdir\n\t\tfile.ext"
when processing substring "subdir2", path[0] = 0, path[1] = 4(dir/), path[2] = 12(dir/subdir1/),
path[2] gets overwrite to 11(dir/subdir/).
1 public class Solution { 2 public int lengthLongestPath(String input) { 3 int result = 0; 4 int[] path = new int[input.length() + 2]; 5 String[] st = input.split("\n"); 6 for (String line : st) { 7 String name = line.replaceAll("(\t)+", ""); 8 int depth = line.length() - name.length(); 9 if(name.contains(".")) { 10 result = Math.max(result, path[depth] + name.length()); 11 } 12 else { 13 path[depth + 1] = path[depth] + name.length() + 1; 14 } 15 } 16 return result; 17 } 18 }
Key Notes: \t is an escape sequence, not two separate characters '\' and 't'