10 lines and you are done-c++


#1

string Solution::simplifyPath(string path) {
string res, tmp;
vector stk;
stringstream ss(path);
while(getline(ss,tmp,’/’)) {
if (tmp == “” or tmp == “.”) continue;
if (tmp == “…” and !stk.empty()) stk.pop_back();
else if (tmp != “…”) stk.push_back(tmp);
}
for(auto str : stk) res += “/”+str;
return res.empty() ? “/” : res;

}