Skip to main content

71.js

/** ------------------------------------------------------------------------------
*
* 2023-04-12
* 71. Simplify Path
* https://leetcode.com/problems/simplify-path/description/
*
------------------------------------------------------------------------------ */
/**
* @param {string} path
* @return {string}
*/
var simplifyPath = function (path) {
const stack = [];

const directories = path.split("/");

for (const dir of directories) {
if (dir === "." || !dir) {
} else if (dir === "..") {
if (stack.length > 0) {
stack.pop();
}
} else {
stack.push(dir);
}
}

return "/" + stack.join("/");
};

// console.log(simplifyPath("/home/"));
// console.log(simplifyPath("/a/./b/../../c/"));
console.log(simplifyPath("/home//foo/"));