DEV Community

Rakesh Reddy Peddamallu
Rakesh Reddy Peddamallu

Posted on

Leetcode - 71. Simplify Path

Using Stack

/**
 * @param {string} path
 * @return {string}
 */
var simplifyPath = function (path) {

    let myStack = [];

    for (char of path.split("/")) {

        if (char == "" || char == ".") {
            continue
        }
        if (char == "..") {
            if (myStack.length > 0) {
                myStack.pop(); // Only pop if there's something to pop
            }
        } else {
            myStack.push(char)
        }

    }
    return "/" + myStack.join("/")
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →