-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
Copy pathLongestAbsolutePath.java
49 lines (43 loc) · 1.27 KB
/
LongestAbsolutePath.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package problems.medium;
import java.util.HashMap;
import java.util.Map;
/**
* Created by sherxon on 4/27/17.
*/
public class LongestAbsolutePath {
public int lengthLongestPath(String input) {
if (input == null || !input.contains("."))
return 0;
Map<Integer, String> map = new HashMap<>();
String[] lines = input.split("\n");
int max = 0;
for (int i = 0; i < lines.length; i++) {
String current = lines[i];
int tabCount = getTabCount(current);
if (current.contains(".")) {
int size = getAbsPathSize(map, tabCount);
max = Math.max(max, size + current.substring(tabCount).length());
} else {
map.put(tabCount, current.substring(tabCount));
}
}
return max;
}
private int getAbsPathSize(Map<Integer, String> map, int ts) {
int size = 0;
for (int i = 0; i < ts; i++) {
size += map.get(i).length() + 1;
}
return size;
}
private int getTabCount(String current) {
int i = 0;
while (i < current.length()) {
if (current.charAt(i) == '\t')
i++;
else
return i;
}
return i;
}
}