knakul853

Untitled

Jun 20th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. /**
  2. knakul853
  3. **/
  4.  
  5. class Solution {
  6. public:
  7.     string simplifyPath(string path) {
  8.        
  9.         string res, temp;
  10.         vector<string>st;
  11.         istringstream in(path);
  12.        
  13.         string word;
  14.        
  15.         while( getline(in, word, '/') )
  16.         {
  17.            if( word == "" || word == "." ) continue;
  18.            
  19.             if( word == ".." && !st.empty() ) st.pop_back();
  20.             else if(word != "..")st.push_back(word);
  21.         }
  22.        
  23.         for(string s:st) res += "/" + s;
  24.        
  25.         return st.empty() ? "/" : res;
  26.     }
  27. };
Add Comment
Please, Sign In to add comment