Posted on

Description

Submission

class Solution {
public:
    string simplifyPath(string path) {
        vector<string> paths;
        
        for(int i = 0; i < path.size();) {
            if(path[i] == '/') {
                ++i;
                continue;
            }
            int end = path.find('/', i);
            string p;
            if(end == string::npos) {
                p = path.substr(i, path.size() - i);
            }
            p = path.substr(i, end - i);
            i += (end - i);
            if(p == ".") continue;
            else if(p == "..") {
                if(!paths.empty()) paths.pop_back();
            } else {
                paths.push_back(p);
            }
        }
        if(paths.empty()) return "/";
        string res = "";
        for(int i = 0; i < paths.size(); ++i) {
            res += '/';
            res += paths[i];
        }
        return res;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *