Skip to content

Latest commit

 

History

History
18 lines (17 loc) · 465 Bytes

File metadata and controls

18 lines (17 loc) · 465 Bytes
class Solution {
public:
    string simplifyPath(string path) {
        string ret, dir;
        stringstream ss(path);
        vector<string> v;
        while (getline(ss, dir, '/')) {
            if (dir == "." || dir == "") continue;
            else if (dir == ".." && !v.empty()) v.pop_back();
            else if (dir != "..") v.push_back(dir);
        }
        for (string s : v) ret += "/" + s;
        return ret.empty() ? "/" : ret;
    }
};