<p>Given the array <code>favoriteCompanies</code> where <code>favoriteCompanies[i]</code> is the list of favorites companies for the <code>ith</code> person (<strong>indexed from 0</strong>).</p>

<p><em>Return the indices of people whose list of favorite companies is not a <strong>subset</strong> of any other list of favorites companies</em>. You must return the indices in increasing order.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> favoriteCompanies = [[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;],[&quot;google&quot;,&quot;microsoft&quot;],[&quot;google&quot;,&quot;facebook&quot;],[&quot;google&quot;],[&quot;amazon&quot;]]
<strong>Output:</strong> [0,1,4] 
<strong>Explanation:</strong> 
Person with index=2 has favoriteCompanies[2]=[&quot;google&quot;,&quot;facebook&quot;] which is a subset of favoriteCompanies[0]=[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;] corresponding to the person with index 0. 
Person with index=3 has favoriteCompanies[3]=[&quot;google&quot;] which is a subset of favoriteCompanies[0]=[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;] and favoriteCompanies[1]=[&quot;google&quot;,&quot;microsoft&quot;]. 
Other lists of favorite companies are not a subset of another list, therefore, the answer is [0,1,4].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> favoriteCompanies = [[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;],[&quot;leetcode&quot;,&quot;amazon&quot;],[&quot;facebook&quot;,&quot;google&quot;]]
<strong>Output:</strong> [0,1] 
<strong>Explanation:</strong> In this case favoriteCompanies[2]=[&quot;facebook&quot;,&quot;google&quot;] is a subset of favoriteCompanies[0]=[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;], therefore, the answer is [0,1].
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> favoriteCompanies = [[&quot;leetcode&quot;],[&quot;google&quot;],[&quot;facebook&quot;],[&quot;amazon&quot;]]
<strong>Output:</strong> [0,1,2,3]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= favoriteCompanies.length &lt;= 100</code></li>
	<li><code>1 &lt;= favoriteCompanies[i].length &lt;= 500</code></li>
	<li><code>1 &lt;= favoriteCompanies[i][j].length &lt;= 20</code></li>
	<li>All strings in <code>favoriteCompanies[i]</code> are <strong>distinct</strong>.</li>
	<li>All lists of favorite companies are <strong>distinct</strong>, that is, If we sort alphabetically each list then <code>favoriteCompanies[i] != favoriteCompanies[j].</code></li>
	<li>All strings consist of lowercase English letters only.</li>
</ul>

class Solution {
public:
    vector<int> peopleIndexes(vector<vector<string>>& favoriteCompanies) {
        // turn this into a vector of sets
        // iterate through each person, keeping a list of all the potential people they could be a subset of
        // go through each company in the person's list, and keep the companies that have that company too
        // if at the end, there are still companies left in the set, the person could be a subset of another one

        // n = number of lists, m = number of elements in a list
        // complexity: O(m * n * n)

        int n = favoriteCompanies.size();
        vector<unordered_set<string>> favoriteSets;
        vector<int> result;

        // turn the lists of favourite companies into sets of favourite companies 
        for(int i = 0; i < n; i++){
            unordered_set<string> temp;

            for(string s : favoriteCompanies[i]){
                temp.insert(s);
            }

            favoriteSets.push_back(temp);
        }

        for(int i = 0; i < n; i++){
            unordered_set<int> potential;

            // all the potential lists this person's list could be a subset of
            for(int j = 0; j < n; j++){
                if(i == j){
                    continue;
                }

                potential.insert(j);
            }

            for(string company : favoriteCompanies[i]){
                unordered_set<int> deleted;

                for(int k : potential){
                    // one of the companies in the current list doesn't exist in the other list; can't be a subset
                    if(favoriteSets[k].find(company) == favoriteSets[k].end() && potential.find(k) != potential.end()){
                        deleted.insert(k);
                    }
                }

                for(int k : deleted){
                    potential.erase(k);
                }
            }

            // none of the potential subsets worked out; this person's list is not a subset of anyone else's list
            if(potential.empty()){
                result.push_back(i);
            }
        }

        sort(result.begin(), result.end());
        return result;
    }
};
162300