Simple 2 pass C++ approach


#1
string Solution::simplifyPath(string A) {
    // to generalize inputs
    if(A[A.size()-1]!='/')      A.append("/");
    vector<string> vec;
    vector<int> index;
    string res = "";
    // store indices of '/' for dir name extraction
    for(int i=0; i<A.length(); i++)
        if(A[i] == '/')     index.push_back(i);
    int st = index[0], en = index[0];
    for(int i=1; i<index.size(); i++){
        st = index[i-1];
        en = index[i];
        string temp = A.substr(st, en-st);
        if(temp == "/." || temp == "/") continue;
        if(temp == "/..") {
            if(vec.size())  vec.pop_back();
        } 
        else
            vec.push_back(temp);
    }
    for(auto it: vec)
        res.append(it);
    return res == "" ? "/" : res;
}

#2

that’s a really good approach!! Amazing!