LeetCode- Longest Absolute File Path

LiBlog發表於2016-08-24

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.

Note:

  • 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 path aaaaaaaaaaaaaaaaaaaaa/sth.png.

Solution:

 1 public class Solution {
 2     public int lengthLongestPath(String input) {
 3         Deque<Integer> lenList = new ArrayDeque<Integer>();
 4         // IMPORTANT: for root dir, lenList.peekLat() is null pointer. 
 5         lenList.add(0); 
 6         // Get name List
 7         String[] nameList = input.split("\n");        
 8         int maxLen = 0;
 9         for (String name : nameList){
10             // Count level
11             int level = getLevel(name);
12             
13             // Pop lenList to corrent level
14             while (lenList.size()-1 > level){
15                 lenList.removeLast();
16             }
17             
18             // Put current total len into lenList
19             lenList.add(lenList.peekLast() + name.length() - level);
20             
21             if (name.contains(".")){
22                 maxLen = Math.max(maxLen,lenList.peekLast()+lenList.size()-2);
23             }
24         }
25         return maxLen;
26     }
27     
28     public int getLevel(String name){
29         return name.lastIndexOf('\t')+1;
30     }
31 }

Solution 2:

Do not need to be so complex, but good for practicing string processing.

 1 public class Solution {
 2     public class Pair<T1, T2> {
 3         T1 first;
 4         T2 second;
 5 
 6         public Pair(T1 f, T2 s) {
 7             first = f;
 8             second = s;
 9         }
10     }
11 
12     public int lengthLongestPath(String input) {
13         if (input.isEmpty())
14             return 0;
15 
16         int next = 0;
17         Deque<String> nameList = new LinkedList<String>();
18         int maxLen = 0;
19         while (next < input.length()) {
20             // Get level
21             Pair<Integer, Integer> levelPair = getLevel(input, next);
22             int level = levelPair.first;
23             next = levelPair.second;
24 
25             // If the level is smaller than current, we need pop.
26             while (nameList.size() > level) {
27                 nameList.removeLast();
28             }
29 
30             // Get name
31             Pair<String, Boolean> namePair = getName(input, next);
32             String name = namePair.first;
33             boolean isFile = namePair.second;
34             next += name.length();
35 
36             // Add name
37             nameList.add(name);
38 
39             // If current one is a file, then get its len.
40             if (isFile) {
41                 int len = getPathLen(nameList);
42                 maxLen = Math.max(maxLen, len);
43             }
44         }
45         return maxLen;
46     }
47 
48     // Get path len
49     public int getPathLen(Deque<String> nameList) {
50         int len = 0;
51         for (String name : nameList) {
52             len += name.length();
53         }
54         // count '/'
55         len += nameList.size() - 1;
56         return len;
57     }
58 
59     // Get the level of next
60     public Pair<Integer, Integer> getLevel(String input, int next) {
61         // Address root level
62         if (input.charAt(next) != '\n') {
63             return new Pair<Integer, Integer>(0, next);
64         }
65         // skip "\n"
66         next++;
67         int level = 0;
68         while (next < input.length() && input.charAt(next) == '\t') {
69             level++;
70             next++;
71         }
72         return new Pair<Integer, Integer>(level, next);
73     }
74 
75     // Get the name of next
76     public Pair<String, Boolean> getName(String input, int next) {
77         boolean isFile = false;
78         StringBuilder builder = new StringBuilder();
79         while (next < input.length() && input.charAt(next) != '\n') {
80             builder.append(input.charAt(next));
81             if (input.charAt(next) == '.')
82                 isFile = true;
83             next++;
84         }
85         return new Pair<String, Boolean>(builder.toString(), isFile);
86     }
87 }

 

相關文章