Skip to content

Commit ee66c5a

Browse files
authored
Create 388. Longest Absolute File Path.java
not the most elegant approach, will improve in time hopefully
1 parent b5c71bb commit ee66c5a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

388. Longest Absolute File Path.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class Solution {
2+
public int lengthLongestPath(String input) {
3+
String [] entities = input.split("\n");
4+
int maxLen = 0;
5+
int curLvl = 0;
6+
String curPath = "";
7+
8+
for (int i = 0; i < entities.length; i++) {
9+
int tab = entities[i].length() - entities[i].replace("\t", "").length();
10+
11+
// adjustment to current path
12+
while (tab < curLvl) {
13+
curPath = curPath.substring(0, curPath.lastIndexOf("/"));
14+
curLvl--;
15+
}
16+
17+
// add dir/file to path
18+
curPath += "/" + entities[i].replace("\t", "");
19+
curLvl++;
20+
21+
System.out.println(curPath);
22+
23+
if ((curPath.length() > maxLen) && curPath.contains(".")) {
24+
maxLen = curPath.length();
25+
}
26+
}
27+
28+
if (maxLen > 0) {
29+
return (maxLen-1);
30+
} else {
31+
return 0;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)