`"dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"`

the root dir `dir`

is in level 1, we push a initial value `0`

into stack first indicating a not existing file with length 0

```
public int lengthLongestPath(String input) {
ArrayDeque<Integer> stack = new ArrayDeque<>();
stack.push(0);
int result = 0;
for (String s : input.split("\n")) {
int level = s.lastIndexOf('\t') + 1;
int len = s.length() - level;
while (stack.size() > level + 1) {
stack.pop();
}
if (s.contains(".")) {
result = Math.max(result, stack.peek() + len);
} else {
stack.push(stack.peek() + len + 1);
}
}
return result;
}
```

hashMap stores (level, the length of the path up to level `level`

) pairs. By default, we use a (0,0) to initialize the hashmap. But for example `"dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"`

.

dir is in level 1, not 0. subdir1 is in level 2, so on...

we update the hashMap using **hashMap.get(level) + len + 1** because the current level is level+1, previous level is level, we `+1`

because the additional path separator char `\`

, if s contains `.`

, we update the current max length

Hope helps

```
public int lengthLongestPath(String input) {
HashMap<Integer, Integer> hashMap = new HashMap<>();
hashMap.put(0, 0);
int result = 0;
for (String s : input.split("\n")) {
int level = s.lastIndexOf('\t') + 1;
int len = s.length() - level;
if (s.contains(".")) {
result = Math.max(result, hashMap.get(level) + len);
} else {
hashMap.put(level + 1, hashMap.get(level) + len + 1);
}
}
return result;
}
```