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

Imagine you're analyzing employee preferences in a corporate survey! You have collected data about each person's favorite companies, and now you need to find the unique individuals whose preferences aren't completely covered by someone else's broader list.

Given an array favoriteCompanies where favoriteCompanies[i] represents the list of favorite companies for the i-th person (0-indexed), your task is to identify people whose company preferences are not a subset of any other person's list.

Goal: Return the indices of people whose favorite companies list is NOT completely contained within anyone else's list. The result should be in increasing order.

Example: If Person A likes ["Google", "Apple"] and Person B likes ["Google", "Apple", "Microsoft"], then Person A's preferences are a subset of Person B's, so we wouldn't include Person A in our result.

Input & Output

example_1.py — Basic Example
$ Input: [["leetcode","google","facebook"],["google","microsoft"],["google","facebook"],["google"],["amazon"]]
Output: [0,1,4]
💡 Note: Person 0's list ["leetcode","google","facebook"] is not a subset of any other list. Person 1's list ["google","microsoft"] is not a subset of any other list. Person 2's list ["google","facebook"] is a subset of Person 0's list. Person 3's list ["google"] is a subset of Person 0's, 1's and 2's lists. Person 4's list ["amazon"] is not a subset of any other list.
example_2.py — All Unique
$ Input: [["leetcode","google","facebook"],["leetcode","amazon"],["facebook","google"]]
Output: [0,1,2]
💡 Note: None of the lists are subsets of each other, so all people should be included in the result.
example_3.py — Single Elements
$ Input: [["leetcode"],["google"],["facebook"],["leetcode","google"]]
Output: [3]
💡 Note: Person 0's ["leetcode"], Person 1's ["google"], and Person 2's ["facebook"] are all subsets of Person 3's ["leetcode","google"]. Only Person 3 has a list that's not a subset of anyone else's.

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
Chef APizza, PastaChef BPizza, PastaSalad, SoupChef CSushi, Ramen❌ Chef A is subset of Chef B✅ Chef C is uniqueHash Set Solution Process1. Convert each recipe collection to hash set for O(1) lookups2. For each chef, check if their recipes exist in other chefs' sets3. Return chefs whose collections aren't subsets of othersResult: [1, 2] (Chef B and Chef C)Chef A's recipes are all contained in Chef B's collection
Understanding the Visualization
1
Collect All Recipes
Each person has their favorite recipes in a collection
2
Convert to Quick-Lookup
Transform each collection into a hash table for fast searching
3
Compare Collections
For each person, check if all their recipes exist in someone else's collection
4
Find Unique Collections
Keep only people whose recipe collections aren't subsets of others
Key Takeaway
🎯 Key Insight: Hash sets transform expensive O(m) string searches into O(1) lookups, making subset checking much more efficient while maintaining the same overall algorithm structure.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
28.5K Views
Medium Frequency
~15 min Avg. Time
892 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