C++ unordered_map, parse with stringstream


  • 0
    B
    class Solution {
    public:
        vector<vector<string>> findDuplicate(vector<string>& paths) {
            unordered_map<string, vector<string>>data;
            for(auto e : paths)
            {
                stringstream ss(e);
                string path, buff;
                vector<string>contents;
                ss >> path;
                while(ss >> buff) contents.push_back(buff);
                for(auto f : contents)
                {
                    f.pop_back();
                    int idx = 0;
                    while(f[idx] != '(') idx++;
                    data[f.substr(idx+1)].push_back(path + "/" +f.substr(0,idx));
                }
            }
            
            vector<vector<string>>ans;
            for(auto e : data)
                if(e.second.size() > 1)
                    ans.push_back(e.second);
            return ans;
        }
    };

Log in to reply
 

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