C++ easy and straight-forward solution


  • 0
    M
        vector<vector<string>> findDuplicate(vector<string>& paths) {
            vector<vector<string>> res;
            if(paths.size()==0||paths[0].size()==0) return res;
            map<string, vector<string>> p;
            for(int i=0;i<paths.size();i++) {
                string p0, p1, c;
                int j=0;
                for(j;paths[i][j]!=' ';j++) p0.append(1, paths[i][j]);
                j++;
                while(j<paths[i].size()) {
                    for(j;paths[i][j]!='(';j++) p1.append(1, paths[i][j]);
                    while(paths[i][++j]!=')') c.append(1, paths[i][j]);
                    if(p.find(c)==p.end()) p[c]={p0+"/"+p1};
                    else p[c].push_back(p0+"/"+p1);
                    p1.clear(), c.clear();
                    j+=2;
                }
            }
            for(map<string, vector<string>>::iterator it=p.begin();it!=p.end();it++) {
                if(it->second.size()>1) res.push_back(it->second);
            }
            return res;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.