# Javascript ES6 Solution with Explanation

• This problem has a few things to keep track of, most importantly the `maxLength`, the `depth` we are currently at, and the maximum `pathLength` at any given depth.

We start the problem by splitting the input by `\n` to give us a clean representation of each level of the file tree, and initialize a `maxLength` to keep track of our maximum length as well as a `pathLength` object to keep track of the length of the path to our current depth.

When we loop over a string in our array, we produce a new string with all instances of `\t` removed, and subtract the length of our new string from the length of our old string to get our current `depth`;

If the string contains a `.` we know that it is a filename and we compare `maxLength` to the length of our new string (without the `\t`'s) added to the `pathLength` of our current depth, and set maxLength equal to that if it is higher.

Finally, if the current string does not contain `.`, that means that it is a folder or subfolder, so we set `pathLength[depth + 1]` equal to the the current pathLength (since that is how many characters it took us to get to this level) added to the length of the current name + 1. We need to add `+1` because the next path will have an additional tab character that we need to account for.

``````const lengthLongestPath = (input) => {
input = input.split('\n');
let maxLength = 0;
let pathLength = {0 : 0};
for (let i = 0; i < input.length; i++) {
const name = input[i].replace(/\t/g, '');
const depth = input[i].length - name.length
if (name.indexOf('.') !== -1) {
maxLength = Math.max(result, pathLength[depth] + name.length)
}
else {
pathLength[depth + 1] = pathLength[depth] + (name.length + 1)
}
}
return maxLength
};
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.