LeetCode 71. Simplify Path

/**
 * @param {string} path
 * @return {string}
 */
var simplifyPath = function(path) {
    const simple = [];
    const splitPath = path.split("/").filter(x => x.length>0 && x !== '.')

    for (let i = 0; i<splitPath.length; i++) {
        if (splitPath[i] === '..') {
            simple.pop()
        } else {
            simple.push(splitPath[i])
        }

    }

    return `/${simple.join('/')}`
};
Published on

Previous post: Ask HN: What is your money-making side project outside programming?

Next post: LeetCode 49. Group Anagrams