https://leetcode.com/problems/simplify-path/description/?envType=study-plan-v2&envId=top-interview-150

class Solution {
public:
    string simplifyPath(string path) {
        int n = path.size();
        vector<string> st;
        int idx = 0;
        while (idx < n) {
            while (idx < n && path[idx] == '/') idx += 1;
            string tmp = "";
            while (idx < n && path[idx] != '/') {
                tmp.push_back(path[idx]);
                idx += 1;
            }
            if (tmp == ".") continue;
            else if (tmp == "..") {
                if (st.size()) st.pop_back();
            } else if (tmp != "") {
                st.push_back(tmp);
            }
        }
        string res = "";
        for (auto &x: st) res += "/" + x;
        return res == "" ? "/" : res;
    }
};

Time Complexity: O(n)
Space: O(n)