Untitled
unknown
plain_text
7 months ago
965 B
8
Indexable
class Solution {
public:
string simplifyPath(string path) {
int n=path.size();
vector<string> stk;
int i=0;
while(i<n)
{
while(i<n && path[i]=='/')
{
++i;
}
int j=i;
string str;
while(j<n && path[j]!='/')
{
str+=path[j++];
}
if(str!=".")
{
if(str==".." && !stk.empty())
{
stk.pop_back();
}
else if(str!="..")
{
stk.push_back(str);
}
}
i=j;
}
if(stk.empty())
{
return "/";
}
string ans;
for(string ch:stk)
{
cout<<ch<<endl;
ans+='/'+ch;
}
return ans;
}
};Editor is loading...
Leave a Comment