Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
711 B
1
Indexable
class Solution {
public:
    string simplifyPath(string path) {
        string tmp="";
        stack<string> s;
        for(auto i:path+"/"){
            if(i=='/'){
                if(tmp==".."){
                    if(!s.empty()) s.pop();
                }
                else if(tmp!="." && tmp!=""){ //tmp!="" checks for double slash ,if the prev char was / then tmp wil be empty
                    s.push(tmp);
                }
                tmp="";
            }else{
                tmp=tmp+i;
            }
        }
        string res="";
        while(!s.empty()){
            res="/" +s.top() +res;
            s.pop();
        }
        if(!res.empty()) return res;
        return "/";
    }
};