3 lines JavaScript - Don't do this in interviews


  • 2

    Standard stack-based solution, compressed just to make it into 3 lines. Don't do this in interviews.

    var simplifyPath = function(path) {
        const stack = [], tokens = path.split('/').filter(t => !['/', '', '.'].includes(t));
        tokens.forEach(t => t === '..' ? stack.pop() : stack.push(t));
        return '/' + stack.join('/');
    };
    

Log in to reply
 

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