forked from pxu/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestAbsoluteFilePath.java
More file actions
35 lines (25 loc) · 1.15 KB
/
LongestAbsoluteFilePath.java
File metadata and controls
35 lines (25 loc) · 1.15 KB
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
import java.util.*;
public class LongestAbsoluteFilePath {
public static void main(String[] args) {
LongestAbsoluteFilePath la = new LongestAbsoluteFilePath();
String input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext";
int result = la.lengthLongestPath("dir\n file.txt".replaceAll(" ", "\t"));
System.out.println(result);
}
public int lengthLongestPath(String input) {
String[] tokens = input.split("\n");
Stack<Integer> pathLen = new Stack<> ();
int result = 0;
for(String s: tokens) {
int level = s.lastIndexOf("\t") + 2;
// System.out.println(level);
while(!pathLen.empty() && pathLen.size() >= level) pathLen.pop();
int currPathLen = pathLen.empty()? (s.length() - level + 2): (pathLen.peek() + s.length() - level + 2);
pathLen.push(currPathLen);
// System.out.println(pathLen);
if(s.indexOf(".") >= 0) result = Math.max(result, currPathLen - 1);
//System.out.println(pathLen);
}
return result;
}
}