<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) {
        int id = 0;
        unordered_map<string, int> companyId;
        for(auto person: favoriteCompanies) {
            for(auto company: person) {
                if(companyId.find(company) == companyId.end()) {
                    companyId[company] = id++;
                }
            }
        }
        int n = favoriteCompanies.size();
        vector<vector<int>> ids(n);
        for(int i =0; i<n ; i++) {
            for(int j =0 ; j < favoriteCompanies[i].size(); j++) {
                ids[i].push_back(companyId[favoriteCompanies[i][j]]);
            }
            sort(ids[i].begin(), ids[i].end());
        }
        vector<int> ans;
        for(int i =0; i< n; i++) {
            bool ok = true;
            for(int j =0; j<n; j++) {
                if(i==j) continue;
                // check iff arr1 exists in arr2
                if(checkExists(ids[j], ids[i])) {
                    ok = false;
                    break;
                }
            }
            if(ok) ans.push_back(i);
        }
        return ans;
    }
    bool checkExists(vector<int>& arr1, vector<int>& arr2) {
        if( arr2.size() > arr1.size()) return false;
        int i1 = 0, i2 = 0;
        while( i1 < arr1.size() && i2 < arr2.size()) {
            if(arr1[i1] == arr2[i2]) i1++, i2++;
            else {
                while( i1 < arr1.size() && arr1[i1] != arr2[i2]) i1++;
            }
        }
        return i2 == arr2.size();
    }
};

/*

100 persons
500 company per person
company name <= 20 chars

*/
101300