388 Longest Absolute File Path
Problem:
dir
subdir1
subdir2
file.extdir
subdir1
file1.ext
subsubdir1
subdir2
subsubdir2
file2.extSolutions:
Last updated
dir
subdir1
subdir2
file.extdir
subdir1
file1.ext
subsubdir1
subdir2
subsubdir2
file2.extLast updated
public class Solution {
public int lengthLongestPath(String input) {
if (input == null || input.length() == 0) {
return 0;
}
String[] tokens = input.split("\n");
int[] dirs = new int[tokens.length];
int max = 0;
int curr;
for (int i = 0; i < tokens.length; i ++) {
String s = tokens[i];
int count = s.lastIndexOf("\t") + 1;
s = s.substring(count);
if (s.indexOf(".") != -1) {
//file
int cand = s.length();
if (count != 0) {
cand = dirs[count - 1] + s.length();
}
max = Math.max(max, cand);
}
else {
//folder
dirs[count] = s.length() + 1;
if (count != 0) {
dirs[count] += dirs[count - 1];
}
}
}
return max;
}
}