Clean C++ solutions using only istringstream functions


  • 0
    H

    Though there are existing solutions using istringstream, it mostly getline the string and then use find and substr on it.
    This solution uses istringstream methods/functions directly, and I think it is cleaner :)

    vector<vector<string>> findDuplicate(vector<string>& paths) {
        unordered_map<string, vector<string>> m;
        string folder, fileName, fileContent;
        for(auto& path : paths){
            istringstream iss(path);
            iss >> folder >> ws;
            while(iss.peek() != EOF) {
                getline(iss, fileName, '(');
                getline(iss, fileContent, ')') >> ws;
                m[fileContent].emplace_back(folder.substr().append("/").append(fileName));
            }
        }
        
        vector<vector<string>> result;
        for(auto& p : m)
            if(p.second.size() > 1)
                result.emplace_back(move(p.second));
        return result;
    }

Log in to reply
 

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