Sparse Arrays

  • + 0 comments

    My Solution for C++ :

    vector<int> matchingStrings(vector<string> strings, vector<string> queries) {
        vector<int> ret;
        unordered_map<string, int> m;
        for (const string &s : strings) {
            m[s]++;
        }
        for (const string &q : queries) {
            if (m[q])
                ret.push_back(m[q]);
            else
                ret.push_back(0);
        }
        return (ret);
    }