Posted on

Description

Submission

class Solution {
public:
    vector<int> peopleIndexes(vector<vector<string>>& favoriteCompanies) {
        int n = favoriteCompanies.size();

        unordered_map<string, bitset<100>> c2p;
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < favoriteCompanies[i].size(); ++j) {
                c2p[favoriteCompanies[i][j]][i] = 1;
            }
        }

        vector<int> rets;
        // for each person
        for(int i = 0; i < n; ++i) {
            bitset<100> bs;
            bs.set();

            // for all the companies for the person
            // find the union set
            for(string c: favoriteCompanies[i]) {
                bs &= c2p[c];
            }

            // if there's only 1 bit left, i.e. only the person
            if(bs.count() == 1) {
                rets.push_back(i);
            }
        }

        return rets;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *