5 line js solution


  • 1
    L
    function simplifyPath(path) {
        const stack = [];
        
        for (let p of path.split('/').filter(p => p && p !== '.')) {
            p === '..' ? stack.pop() : stack.push(p);
        }
        
        return '/' + stack.join('/');
    }
    

  • 0
    L

    or

    function simplifyPath(path) {
        const stack = [];
        const pathes = path.split('/').filter(p => p && p !== '.');
        pathes.map(p => p === '..' ? stack.pop() : stack.push(p));
        return '/' + stack.join('/');
    }
    

Log in to reply
 

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