forked from ganjingcatherine/Lintcode_HighFreq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path643.system-longest-file-path.java
More file actions
28 lines (25 loc) · 919 Bytes
/
643.system-longest-file-path.java
File metadata and controls
28 lines (25 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Solution {
/*
* @param input: an abstract file system
* @return: return the length of the longest absolute path to file
*/
public int lengthLongestPath(String input) {
if (input == null || input.length() == 0) {
return 0;
}
String[] lines = input.split("\n");
int[] levels = new int[input.length() + 1];
int ans = 0;
for (String line : lines) {
int level = line.lastIndexOf('\t') + 2; // offset level by 2
// int len = line.length() - ( line.lastIndexof("\t") + 1);
int len = line.length() - (level - 1);
if (line.contains(".")) { // this is a file
ans = Math.max(ans, levels[level - 1] + len);
} else {
levels[level] = levels[level - 1] + len + 1;
}
}
return ans;
}
}