-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLongestAbsoluteFilePath.java
38 lines (32 loc) · 1.02 KB
/
LongestAbsoluteFilePath.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
package com.smlnskgmail.jaman.leetcodejava.medium;
// https://leetcode.com/problems/longest-absolute-file-path/
public class LongestAbsoluteFilePath {
private final String input;
public LongestAbsoluteFilePath(String input) {
this.input = input;
}
public int solution() {
int result = 0;
int[] fileLengths = new int[input.length()];
for (String name : input.split("\n")) {
int dirLevel = dirLevel(name);
String cName = name.substring(dirLevel);
int part = dirLevel > 0
? fileLengths[dirLevel - 1] + 1
: 0;
fileLengths[dirLevel] = part + cName.length();
if (cName.contains(".")) {
result = Math.max(result, fileLengths[dirLevel]);
}
}
return result;
}
private int dirLevel(String dirName) {
int level = 0;
int i = 0;
while (dirName.charAt(i++) == '\t') {
level++;
}
return level;
}
}