Simplify Directory Path Time : Space : 1234567891011121314151617181920212223242526vector<string> parse(string A) { vector<string> res; string now = ""; for(int i = 0; i < A.size(); i++) { if(A[i] == '/') { if(now.length()) res.push_back(now); now = ""; } else now.push_back(A[i]); } if(now.length()) res.push_back(now); return res;}string Solution::simplifyPath(string A) { vector<string> token = parse(A); vector<string> simple; for(auto t : token) { if(t == ".") continue; else if(t == "..") { if(!simple.empty()) simple.pop_back(); } else simple.push_back(t); } string res = ""; for(auto& s : simple) res += "/" + s; return res == "" ? "/" : res;}