forked from adamespii/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_017.js
28 lines (23 loc) Β· 876 Bytes
/
problem_017.js
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
/**
* Given a string representing the file system, returns the length of the longest absolute path to a file.
* @param {string} input
* @return {number}
*/
const longestDirectory = (input) => {
let maxLength = 0;
let pathLength = [0];
for(let line of input.split('\n')) {
let name = line.replace(/^\t+/,'');
let depth = line.length - name.length;
// Check for file cases
if (name.includes('.')) {
maxLength = Math.max(maxLength, pathLength[depth] + name.length);
} else {
pathLength[depth + 1] = pathLength[depth] + name.length + 1;
}
}
return maxLength;
}
console.log(longestDirectory("dir\n\tsubdir1")); // 0
console.log(longestDirectory("dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext")); // 20
console.log(longestDirectory("dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext")); // 32