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,1,2]
💡 Note: Person 0 ["leetcode","google","facebook"] is not a subset of any other list. Person 1 ["leetcode","microsoft"] is not a subset of any other list (person 0 is missing "microsoft", person 2 is missing "leetcode"). Person 2 ["google","microsoft"] is not a subset of any other list. So all indices [0,1,2] are returned.
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
Favorite Companies Subset Problem INPUT Person 0: leetcode google facebook Set size: 3 companies Person 1: leetcode microsoft Set size: 2 companies Person 2: google microsoft Set size: 2 companies favoriteCompanies[0..2] 3 people, varying set sizes Convert lists to HashSets for O(1) lookups ALGORITHM STEPS 1 Convert to HashSets Each person's list becomes a HashSet for O(1) lookup 2 Compare All Pairs For each person i, check against all others j (i != j) 3 Subset Check Use set.containsAll() to verify if i is subset of j 4 Collect Results Add index if NOT subset of any other person's list Subset Comparisons: P0 subset of P1? NO (facebook) P0 subset of P2? NO (leetcode) P1 subset of P0? NO (microsoft) P2 subset of P0? NO (microsoft) FINAL RESULT Person 0: NOT a subset "facebook" not in P1's set "leetcode" not in P2's set KEEP Person 1: IS a subset Not subset of P0 or P2... But wait - checking all pairs SKIP Person 2: NOT a subset "microsoft" not in P0's set "google" not in P1's set KEEP Output: [0, 2] Key Insight: HashSet optimization allows O(1) membership checks instead of O(n) linear search. For each person, we check if their entire company set is contained within another person's set. Time Complexity: O(n^2 * m) where n = people count, m = max companies per person. TutorialsPoint - People Whose List of Favorite Companies Is Not a Subset of Another List | Hash Set Optimization
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