| 7 | class Solution { |
| 8 | public: |
| 9 | string simplifyPath(string path) { |
| 10 | string buf; |
| 11 | vector<string> stk; |
| 12 | stringstream ss(path); |
| 13 | while (getline(ss, buf, '/')) { |
| 14 | if (buf == "" || buf == ".") continue; |
| 15 | if (buf == ".." and not stk.empty()) stk.pop_back(); |
| 16 | else if (buf != "..") stk.push_back(buf); |
| 17 | } |
| 18 | string res; |
| 19 | for (auto str: stk) { |
| 20 | res += '/' + str; |
| 21 | } |
| 22 | return res.empty() ? "/" : res; |
| 23 | } |
| 24 | }; |
| 25 | |
| 26 |