# [71\. Simplify Path](https://leetcode.com/problems/simplify-path/) :::spoiler Solution ```cpp= class Solution { public: string simplifyPath(string path) { vector<string> dir; stringstream ss(path); string token; while(getline(ss, token, '/')) { if(token == "..") { if (!dir.empty()) dir.pop_back(); } else if (!token.empty() && token != ".") { dir.push_back(token); } } string res; for (auto s : dir) res += "/" + s; return res.empty() ? "/" : res; } }; ``` - T: $O(n)$ - S: $O(n)$ :::
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up