[609. Find Duplicate File in System] C++_AC_unordered_map


  • 0
    class Solution {
    public:
    vector<vector<string>> findDuplicate(vector<string>& paths) {
        vector<vector<string> > res;
        if(paths.empty()) return res;
        unordered_map<string, vector<string> > mp;
        for(int i = 0; i < paths.size(); ++i){
            string pathname = "";
            int j = 0;
            while(j < paths[i].size() && paths[i][j] != ' '){
                pathname += paths[i][j];
                j++;
            }
            j++;
            while(j < paths[i].size()){
                string filename = "";
                string content = "";
                while(j < paths[i].size() && paths[i][j] != '('){
                    filename += paths[i][j];
                    j++;
                }
                while(j < paths[i].size() && paths[i][j] != ')'){
                    content += paths[i][j];
                    j++;
                }
                mp[content].push_back(pathname +'/'+filename);
                while(j < paths[i].size() && (paths[i][j] == ')' || paths[i][j] == ' ')) j++;
            }
        }
        for(auto m : mp){
            if(m.second.size() > 1){
                res.push_back(m.second);
            }
        }
        return res;
    }
    

    };


Log in to reply
 

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