Untitled

 avatar
unknown
plain_text
9 days ago
965 B
6
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