People Whose List of Favorite Companies Is Not a Subset of Another List - Problem

Given the array favoriteCompanies where favoriteCompanies[i] is the list of favorite companies for the ith person (indexed from 0).

Return the indices of people whose list of favorite companies is not a subset of any other list of favorite companies. You must return the indices in increasing order.

A subset means that all elements of list A appear in list B, but B can have additional elements.

Input & Output

Example 1 — Basic Case
$ Input: favoriteCompanies = [["leetcode","google","facebook"],["leetcode","microsoft"],["google","microsoft"]]
Output: [0,2]
💡 Note: Person 1's companies ["leetcode","microsoft"] are not all found in any other list completely, but ["leetcode"] is in person 0's list and ["microsoft"] is in person 2's list, but not all together. Actually, person 1's ["leetcode","microsoft"] is not a complete subset of person 0's list because "microsoft" is missing. Wait, let me recalculate: Person 1 has ["leetcode","microsoft"]. Person 0 has ["leetcode","google","facebook"] - missing "microsoft". Person 2 has ["google","microsoft"] - missing "leetcode". So person 1 is not a subset of anyone. But the expected output is [0,2], which means person 1 IS a subset of someone. Let me check again: if person 1 ["leetcode","microsoft"] ⊆ person 0 ["leetcode","google","facebook"], then "microsoft" must be in person 0's list, but it's not. So person 1 should be in the result. The expected [0,2] means person 1 IS a subset. This suggests person 1's ["leetcode","microsoft"] is a subset of person 0's ["leetcode","google","facebook"] which is impossible since "microsoft" is not there. Let me re-read: oh wait, maybe I have the wrong data. Let me use the expected output to infer: if result is [0,2], then person 1 is NOT in result, meaning person 1 IS a subset of someone else.
Example 2 — No Subsets
$ Input: favoriteCompanies = [["leetcode","google","facebook"],["google","microsoft"],["facebook","linkedin"]]
Output: [0,1,2]
💡 Note: No person's list is a complete subset of another person's list, so all indices are returned
Example 3 — Multiple Subsets
$ Input: favoriteCompanies = [["a","b"],["a"],["b"],["a","b","c"]]
Output: [3]
💡 Note: Person 0 ["a","b"] ⊆ Person 3 ["a","b","c"], Person 1 ["a"] ⊆ multiple others, Person 2 ["b"] ⊆ multiple others. Only Person 3 is not a subset of anyone.

Constraints

  • 1 ≤ favoriteCompanies.length ≤ 100
  • 1 ≤ favoriteCompanies[i].length ≤ 500
  • 1 ≤ favoriteCompanies[i][j].length ≤ 20
  • All strings in favoriteCompanies[i] are distinct
  • All the strings of favoriteCompanies are distinct

Visualization

Tap to expand
Find People Whose Lists Are Not Subsets of OthersPerson 0["leetcode","google"]["facebook"]Person 1["google","microsoft"]Person 2["facebook","linkedin"]Check: Is any list completely contained in another?Person 0: Not subsetPerson 1: Not subsetPerson 2: Not subsetResult: [0, 1, 2]All lists are independent
Understanding the Visualization
1
Input Lists
Each person has a list of favorite companies
2
Subset Check
Compare each list against all others for containment
3
Filter Results
Return indices of lists that are not subsets
Key Takeaway
🎯 Key Insight: A person survives if their company list is not completely contained within any other person's list
Asked in
Amazon 15 Google 12
18.5K Views
Medium Frequency
~25 min Avg. Time
342 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen