Longest Absolute File Path LeetCode solution in JavaScript
1function isFile(path) {2return path.includes(".");3}45var lengthLongestPath = function (input) {6const segments = input.split("\n");78let max = 0;9let path = [];10for (const segment of segments) {11if (segment.startsWith("\t")) {12const nesting = segment.match(/\t/g).length;1314while (nesting < path.length) {15path.pop();16}1718path.push(segment.replace(/\t/g, ""));19} else {20path = [segment];21}2223if (isFile(path.at(-1))) {24const filePath = path.join("/");25if (filePath.length > max) {26max = filePath.length;27}28}29}3031return max;32};