Longest Absolute File Path LeetCode solution in JavaScript

1
function isFile(path) {
2
return path.includes(".");
3
}
4

5
var lengthLongestPath = function (input) {
6
const segments = input.split("\n");
7

8
let max = 0;
9
let path = [];
10
for (const segment of segments) {
11
if (segment.startsWith("\t")) {
12
const nesting = segment.match(/\t/g).length;
13

14
while (nesting < path.length) {
15
path.pop();
16
}
17

18
path.push(segment.replace(/\t/g, ""));
19
} else {
20
path = [segment];
21
}
22

23
if (isFile(path.at(-1))) {
24
const filePath = path.join("/");
25
if (filePath.length > max) {
26
max = filePath.length;
27
}
28
}
29
}
30

31
return max;
32
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0