Javascript Solution


  • 0
    M
    var lengthLongestPath = function(input) {
        let maxLength = 0;
        let pathLength = { 0: 0 }
        let lines = input.split("\n");
    
        for (let i = 0; i < lines.length; i++) {
            let name = lines[i].replace(/\t/g,"");
            let depth = lines[i].length - name.length;
    
            if (name.includes(".")) {
                maxLength = Math.max(maxLength, pathLength[depth] + name.length);
            } else {
                pathLength[depth + 1] = pathLength[depth] + name.length + 1;
            }
        }
    
        return maxLength;
    };
    

Log in to reply
 

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