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 (cha…


This content originally appeared on DEV Community and was authored by Rakesh Reddy Peddamallu

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("/")
};


This content originally appeared on DEV Community and was authored by Rakesh Reddy Peddamallu


Print Share Comment Cite Upload Translate Updates
APA

Rakesh Reddy Peddamallu | Sciencx (2025-03-21T08:33:28+00:00) Leetcode – 71. Simplify Path. Retrieved from https://www.scien.cx/2025/03/21/leetcode-71-simplify-path/

MLA
" » Leetcode – 71. Simplify Path." Rakesh Reddy Peddamallu | Sciencx - Friday March 21, 2025, https://www.scien.cx/2025/03/21/leetcode-71-simplify-path/
HARVARD
Rakesh Reddy Peddamallu | Sciencx Friday March 21, 2025 » Leetcode – 71. Simplify Path., viewed ,<https://www.scien.cx/2025/03/21/leetcode-71-simplify-path/>
VANCOUVER
Rakesh Reddy Peddamallu | Sciencx - » Leetcode – 71. Simplify Path. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/21/leetcode-71-simplify-path/
CHICAGO
" » Leetcode – 71. Simplify Path." Rakesh Reddy Peddamallu | Sciencx - Accessed . https://www.scien.cx/2025/03/21/leetcode-71-simplify-path/
IEEE
" » Leetcode – 71. Simplify Path." Rakesh Reddy Peddamallu | Sciencx [Online]. Available: https://www.scien.cx/2025/03/21/leetcode-71-simplify-path/. [Accessed: ]
rf:citation
» Leetcode – 71. Simplify Path | Rakesh Reddy Peddamallu | Sciencx | https://www.scien.cx/2025/03/21/leetcode-71-simplify-path/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.