nikunjsoni

71

Apr 7th, 2021
60
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     string simplifyPath(string path) {
  4.         string res, tmp;
  5.         vector<string> stk;
  6.         stringstream ss(path);
  7.         while(getline(ss,tmp,'/')) {
  8.             if(tmp == "" or tmp == ".") continue;
  9.             if(tmp == ".."){
  10.                 if(!stk.empty())
  11.                     stk.pop_back();
  12.             }
  13.             else stk.push_back(tmp);
  14.         }
  15.         for(auto str : stk) res += "/"+str;
  16.         return res.empty() ? "/" : res;
  17.     }
  18. };
RAW Paste Data