Using stack and strtok(str,"/")


#1

string Solution::simplifyPath(string A) {
vectorv;
chartemp = strtok((char)A.c_str(),"/");
while(temp!=NULL){
v.push_back(temp);
temp = strtok(NULL,"/");
}
int i = 0;
stackst;
while(i<v.size()){
if(v[i]!="…"&&v[i]!=".")st.push(v[i]);
else{
if(!st.empty()&&v[i]=="…")st.pop();
}
i++;
}
v.clear();
while(!st.empty()){
v.push_back(st.top());
st.pop();
}
reverse(v.begin(),v.end());
string s = “/”;
if(v.size()>0)
s+=v[0];
for(int i=1;i<v.size();i++){
s+="/"+v[i];
}

return s;

}