-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy path_388_1.java
36 lines (32 loc) · 972 Bytes
/
_388_1.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
/**
* LeetCode 388 - Longest Absolute File Path
* <p>
* Recursive Solution
*/
public class _388_1 {
char[] s;
int ans = 0, ptr = 0;
private int peekLevel() {
int cnt = 0;
for (int i = ptr; i < s.length && s[i] == '\t'; i++, cnt++) ;
return cnt;
}
private void dfs(int currentLevel, int len) {
boolean isFile = false;
for (; s[ptr] != '\n'; ptr++) {
isFile = isFile || s[ptr] == '.';
if (s[ptr] != '\t') len++;
}
if (isFile) ans = Math.max(ans, len);
ptr++; // skip '\n'
len++; // append a '/'
while (ptr < s.length && peekLevel() == currentLevel + 1) dfs(currentLevel + 1, len); // recur on its children
}
public int lengthLongestPath(String input) {
if (!input.endsWith("\n")) input += "\n";
this.s = input.toCharArray();
ans = ptr = 0;
while (ptr < s.length) dfs(0, 0);
return ans;
}
}