diff --git a/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/README.md b/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/README.md index 4226d3de2fc6e..eb98cd651a414 100644 --- a/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/README.md +++ b/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/README.md @@ -29,7 +29,7 @@ tags:
示例 1:
输入:favoriteCompanies = [["leetcode","google","facebook"],["google","microsoft"],["google","facebook"],["google"],["amazon"]] -输出:[0,1,4] +输出:[0,1,4] 解释: favoriteCompanies[2]=["google","facebook"] 是 favoriteCompanies[0]=["leetcode","google","facebook"] 的子集。 favoriteCompanies[3]=["google"] 是 favoriteCompanies[0]=["leetcode","google","facebook"] 和 favoriteCompanies[1]=["google","microsoft"] 的子集。 @@ -39,7 +39,7 @@ favoriteCompanies[3]=["google"] 是 favoriteCompanies[0]=["leetco示例 2:
输入:favoriteCompanies = [["leetcode","google","facebook"],["leetcode","amazon"],["facebook","google"]] -输出:[0,1] +输出:[0,1] 解释:favoriteCompanies[2]=["facebook","google"] 是 favoriteCompanies[0]=["leetcode","google","facebook"] 的子集,因此,答案为 [0,1] 。@@ -70,9 +70,9 @@ favoriteCompanies[3]=["google"] 是 favoriteCompanies[0]=["leetco ### 方法一:哈希表 -将每个 `company` 字符串列表都转换为一个整数类型的集合。然后遍历每个集合,判断其是否是其他集合的子集,如果不是,则将其下标加入结果集。 +我们可以将每个公司映射到一个唯一的整数,然后对于每个人,我们将他们收藏的公司转换为整数集合,最后判断是否存在一个人的收藏公司是另一个人的子集。 -时间复杂度 $O(n^2 \times m)$,其中 $n$ 为 `favoriteCompanies` 的长度,$m$ 为 `favoriteCompanies[i]` 的最大长度。 +时间复杂度 $(n \times m \times k + n^2 \times m)$,空间复杂度 $O(n \times m)$。其中 $n$ 和 $m$ 分别是 `favoriteCompanies` 的长度和每个公司清单的平均长度,而 $k$ 是每个公司的平均长度。 @@ -81,25 +81,19 @@ favoriteCompanies[3]=["google"] 是 favoriteCompanies[0]=["leetco ```python class Solution: def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]: - d = {} idx = 0 - t = [] - for v in favoriteCompanies: - for c in v: - if c not in d: - d[c] = idx + d = {} + n = len(favoriteCompanies) + nums = [set() for _ in range(n)] + for i, ss in enumerate(favoriteCompanies): + for s in ss: + if s not in d: + d[s] = idx idx += 1 - t.append({d[c] for c in v}) + nums[i].add(d[s]) ans = [] - for i, nums1 in enumerate(t): - ok = True - for j, nums2 in enumerate(t): - if i == j: - continue - if not (nums1 - nums2): - ok = False - break - if ok: + for i in range(n): + if not any(i != j and (nums[i] & nums[j]) == nums[i] for j in range(n)): ans.append(i) return ans ``` @@ -109,32 +103,26 @@ class Solution: ```java class Solution { public ListpeopleIndexes(List > favoriteCompanies) { + int n = favoriteCompanies.size(); Map
d = new HashMap<>(); int idx = 0; - int n = favoriteCompanies.size(); - Set [] t = new Set[n]; + Set [] nums = new Set[n]; + Arrays.setAll(nums, i -> new HashSet<>()); for (int i = 0; i < n; ++i) { - var v = favoriteCompanies.get(i); - for (var c : v) { - if (!d.containsKey(c)) { - d.put(c, idx++); + var ss = favoriteCompanies.get(i); + for (var s : ss) { + if (!d.containsKey(s)) { + d.put(s, idx++); } + nums[i].add(d.get(s)); } - Set s = new HashSet<>(); - for (var c : v) { - s.add(d.get(c)); - } - t[i] = s; } List ans = new ArrayList<>(); for (int i = 0; i < n; ++i) { boolean ok = true; - for (int j = 0; j < n; ++j) { - if (i != j) { - if (t[j].containsAll(t[i])) { - ok = false; - break; - } + for (int j = 0; j < n && ok; ++j) { + if (i != j && nums[j].containsAll(nums[i])) { + ok = false; } } if (ok) { @@ -152,46 +140,43 @@ class Solution { class Solution { public: vector peopleIndexes(vector >& favoriteCompanies) { + int n = favoriteCompanies.size(); unordered_map d; - int idx = 0, n = favoriteCompanies.size(); - vector > t(n); + int idx = 0; + vector > nums(n); + for (int i = 0; i < n; ++i) { - auto v = favoriteCompanies[i]; - for (auto& c : v) { - if (!d.count(c)) { - d[c] = idx++; + for (const auto& s : favoriteCompanies[i]) { + if (!d.contains(s)) { + d[s] = idx++; } + nums[i].insert(d[s]); } - unordered_set s; - for (auto& c : v) { - s.insert(d[c]); - } - t[i] = s; } + + auto check = [](const unordered_set & a, const unordered_set & b) { + for (int x : a) { + if (!b.contains(x)) { + return false; + } + } + return true; + }; + vector ans; for (int i = 0; i < n; ++i) { bool ok = true; - for (int j = 0; j < n; ++j) { - if (i == j) continue; - if (check(t[i], t[j])) { + for (int j = 0; j < n && ok; ++j) { + if (i != j && check(nums[i], nums[j])) { ok = false; - break; } } if (ok) { ans.push_back(i); } } - return ans; - } - bool check(unordered_set & nums1, unordered_set & nums2) { - for (int v : nums1) { - if (!nums2.count(v)) { - return false; - } - } - return true; + return ans; } }; ``` @@ -199,27 +184,26 @@ public: #### Go ```go -func peopleIndexes(favoriteCompanies [][]string) []int { - d := map[string]int{} - idx, n := 0, len(favoriteCompanies) - t := make([]map[int]bool, n) - for i, v := range favoriteCompanies { - for _, c := range v { - if _, ok := d[c]; !ok { - d[c] = idx +func peopleIndexes(favoriteCompanies [][]string) (ans []int) { + n := len(favoriteCompanies) + d := make(map[string]int) + idx := 0 + nums := make([]map[int]struct{}, n) + + for i := 0; i < n; i++ { + nums[i] = make(map[int]struct{}) + for _, s := range favoriteCompanies[i] { + if _, ok := d[s]; !ok { + d[s] = idx idx++ } + nums[i][d[s]] = struct{}{} } - s := map[int]bool{} - for _, c := range v { - s[d[c]] = true - } - t[i] = s } - ans := []int{} - check := func(nums1, nums2 map[int]bool) bool { - for v, _ := range nums1 { - if _, ok := nums2[v]; !ok { + + check := func(a, b map[int]struct{}) bool { + for x := range a { + if _, ok := b[x]; !ok { return false } } @@ -227,20 +211,61 @@ func peopleIndexes(favoriteCompanies [][]string) []int { } for i := 0; i < n; i++ { ok := true - for j := 0; j < n; j++ { - if i == j { - continue - } - if check(t[i], t[j]) { + for j := 0; j < n && ok; j++ { + if i != j && check(nums[i], nums[j]) { ok = false - break } } if ok { ans = append(ans, i) } } - return ans + + return +} +``` + +#### TypeScript + +```ts +function peopleIndexes(favoriteCompanies: string[][]): number[] { + const n = favoriteCompanies.length; + const d: Map = new Map(); + let idx = 0; + const nums: Set [] = Array.from({ length: n }, () => new Set ()); + + for (let i = 0; i < n; i++) { + for (const s of favoriteCompanies[i]) { + if (!d.has(s)) { + d.set(s, idx++); + } + nums[i].add(d.get(s)!); + } + } + + const check = (a: Set , b: Set ): boolean => { + for (const x of a) { + if (!b.has(x)) { + return false; + } + } + return true; + }; + + const ans: number[] = []; + for (let i = 0; i < n; i++) { + let ok = true; + for (let j = 0; j < n && ok; j++) { + if (i !== j && check(nums[i], nums[j])) { + ok = false; + } + } + if (ok) { + ans.push(i); + } + } + + return ans; } ``` diff --git a/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/README_EN.md b/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/README_EN.md index c0566b5a0c31f..01390eff8d985 100644 --- a/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/README_EN.md +++ b/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/README_EN.md @@ -29,10 +29,10 @@ tags: Input: favoriteCompanies = [["leetcode","google","facebook"],["google","microsoft"],["google","facebook"],["google"],["amazon"]] -Output: [0,1,4] -Explanation: -Person with index=2 has favoriteCompanies[2]=["google","facebook"] which is a subset of favoriteCompanies[0]=["leetcode","google","facebook"] corresponding to the person with index 0. -Person with index=3 has favoriteCompanies[3]=["google"] which is a subset of favoriteCompanies[0]=["leetcode","google","facebook"] and favoriteCompanies[1]=["google","microsoft"]. +Output: [0,1,4] +Explanation: +Person with index=2 has favoriteCompanies[2]=["google","facebook"] which is a subset of favoriteCompanies[0]=["leetcode","google","facebook"] corresponding to the person with index 0. +Person with index=3 has favoriteCompanies[3]=["google"] which is a subset of favoriteCompanies[0]=["leetcode","google","facebook"] and favoriteCompanies[1]=["google","microsoft"]. Other lists of favorite companies are not a subset of another list, therefore, the answer is [0,1,4].@@ -40,7 +40,7 @@ Other lists of favorite companies are not a subset of another list, therefore, tInput: favoriteCompanies = [["leetcode","google","facebook"],["leetcode","amazon"],["facebook","google"]] -Output: [0,1] +Output: [0,1] Explanation: In this case favoriteCompanies[2]=["facebook","google"] is a subset of favoriteCompanies[0]=["leetcode","google","facebook"], therefore, the answer is [0,1].@@ -69,7 +69,11 @@ Other lists of favorite companies are not a subset of another list, therefore, t -### Solution 1 +### Solution 1: Hash Table + +We can map each company to a unique integer. Then, for each person, we convert their favorite companies into a set of integers. Finally, we check if the favorite companies of one person are a subset of another person's favorite companies. + +The time complexity is $(n \times m \times k + n^2 \times m)$, and the space complexity is $O(n \times m)$. Here, $n$ and $m$ are the lengths of `favoriteCompanies` and the average length of each company's list, respectively, and $k$ is the average length of each company. @@ -78,25 +82,19 @@ Other lists of favorite companies are not a subset of another list, therefore, t ```python class Solution: def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]: - d = {} idx = 0 - t = [] - for v in favoriteCompanies: - for c in v: - if c not in d: - d[c] = idx + d = {} + n = len(favoriteCompanies) + nums = [set() for _ in range(n)] + for i, ss in enumerate(favoriteCompanies): + for s in ss: + if s not in d: + d[s] = idx idx += 1 - t.append({d[c] for c in v}) + nums[i].add(d[s]) ans = [] - for i, nums1 in enumerate(t): - ok = True - for j, nums2 in enumerate(t): - if i == j: - continue - if not (nums1 - nums2): - ok = False - break - if ok: + for i in range(n): + if not any(i != j and (nums[i] & nums[j]) == nums[i] for j in range(n)): ans.append(i) return ans ``` @@ -106,32 +104,26 @@ class Solution: ```java class Solution { public ListpeopleIndexes(List > favoriteCompanies) { + int n = favoriteCompanies.size(); Map
d = new HashMap<>(); int idx = 0; - int n = favoriteCompanies.size(); - Set [] t = new Set[n]; + Set [] nums = new Set[n]; + Arrays.setAll(nums, i -> new HashSet<>()); for (int i = 0; i < n; ++i) { - var v = favoriteCompanies.get(i); - for (var c : v) { - if (!d.containsKey(c)) { - d.put(c, idx++); + var ss = favoriteCompanies.get(i); + for (var s : ss) { + if (!d.containsKey(s)) { + d.put(s, idx++); } + nums[i].add(d.get(s)); } - Set s = new HashSet<>(); - for (var c : v) { - s.add(d.get(c)); - } - t[i] = s; } List ans = new ArrayList<>(); for (int i = 0; i < n; ++i) { boolean ok = true; - for (int j = 0; j < n; ++j) { - if (i != j) { - if (t[j].containsAll(t[i])) { - ok = false; - break; - } + for (int j = 0; j < n && ok; ++j) { + if (i != j && nums[j].containsAll(nums[i])) { + ok = false; } } if (ok) { @@ -149,46 +141,43 @@ class Solution { class Solution { public: vector peopleIndexes(vector >& favoriteCompanies) { + int n = favoriteCompanies.size(); unordered_map d; - int idx = 0, n = favoriteCompanies.size(); - vector > t(n); + int idx = 0; + vector > nums(n); + for (int i = 0; i < n; ++i) { - auto v = favoriteCompanies[i]; - for (auto& c : v) { - if (!d.count(c)) { - d[c] = idx++; + for (const auto& s : favoriteCompanies[i]) { + if (!d.contains(s)) { + d[s] = idx++; } + nums[i].insert(d[s]); } - unordered_set s; - for (auto& c : v) { - s.insert(d[c]); - } - t[i] = s; } + + auto check = [](const unordered_set & a, const unordered_set & b) { + for (int x : a) { + if (!b.contains(x)) { + return false; + } + } + return true; + }; + vector ans; for (int i = 0; i < n; ++i) { bool ok = true; - for (int j = 0; j < n; ++j) { - if (i == j) continue; - if (check(t[i], t[j])) { + for (int j = 0; j < n && ok; ++j) { + if (i != j && check(nums[i], nums[j])) { ok = false; - break; } } if (ok) { ans.push_back(i); } } - return ans; - } - bool check(unordered_set & nums1, unordered_set & nums2) { - for (int v : nums1) { - if (!nums2.count(v)) { - return false; - } - } - return true; + return ans; } }; ``` @@ -196,27 +185,26 @@ public: #### Go ```go -func peopleIndexes(favoriteCompanies [][]string) []int { - d := map[string]int{} - idx, n := 0, len(favoriteCompanies) - t := make([]map[int]bool, n) - for i, v := range favoriteCompanies { - for _, c := range v { - if _, ok := d[c]; !ok { - d[c] = idx +func peopleIndexes(favoriteCompanies [][]string) (ans []int) { + n := len(favoriteCompanies) + d := make(map[string]int) + idx := 0 + nums := make([]map[int]struct{}, n) + + for i := 0; i < n; i++ { + nums[i] = make(map[int]struct{}) + for _, s := range favoriteCompanies[i] { + if _, ok := d[s]; !ok { + d[s] = idx idx++ } + nums[i][d[s]] = struct{}{} } - s := map[int]bool{} - for _, c := range v { - s[d[c]] = true - } - t[i] = s } - ans := []int{} - check := func(nums1, nums2 map[int]bool) bool { - for v, _ := range nums1 { - if _, ok := nums2[v]; !ok { + + check := func(a, b map[int]struct{}) bool { + for x := range a { + if _, ok := b[x]; !ok { return false } } @@ -224,20 +212,61 @@ func peopleIndexes(favoriteCompanies [][]string) []int { } for i := 0; i < n; i++ { ok := true - for j := 0; j < n; j++ { - if i == j { - continue - } - if check(t[i], t[j]) { + for j := 0; j < n && ok; j++ { + if i != j && check(nums[i], nums[j]) { ok = false - break } } if ok { ans = append(ans, i) } } - return ans + + return +} +``` + +#### TypeScript + +```ts +function peopleIndexes(favoriteCompanies: string[][]): number[] { + const n = favoriteCompanies.length; + const d: Map = new Map(); + let idx = 0; + const nums: Set [] = Array.from({ length: n }, () => new Set ()); + + for (let i = 0; i < n; i++) { + for (const s of favoriteCompanies[i]) { + if (!d.has(s)) { + d.set(s, idx++); + } + nums[i].add(d.get(s)!); + } + } + + const check = (a: Set , b: Set ): boolean => { + for (const x of a) { + if (!b.has(x)) { + return false; + } + } + return true; + }; + + const ans: number[] = []; + for (let i = 0; i < n; i++) { + let ok = true; + for (let j = 0; j < n && ok; j++) { + if (i !== j && check(nums[i], nums[j])) { + ok = false; + } + } + if (ok) { + ans.push(i); + } + } + + return ans; } ``` diff --git a/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.cpp b/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.cpp index 06b4998384e89..bf9bbc2192ae7 100644 --- a/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.cpp +++ b/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.cpp @@ -1,45 +1,42 @@ class Solution { public: vector peopleIndexes(vector >& favoriteCompanies) { + int n = favoriteCompanies.size(); unordered_map d; - int idx = 0, n = favoriteCompanies.size(); - vector > t(n); + int idx = 0; + vector > nums(n); + for (int i = 0; i < n; ++i) { - auto v = favoriteCompanies[i]; - for (auto& c : v) { - if (!d.count(c)) { - d[c] = idx++; + for (const auto& s : favoriteCompanies[i]) { + if (!d.contains(s)) { + d[s] = idx++; } + nums[i].insert(d[s]); } - unordered_set s; - for (auto& c : v) { - s.insert(d[c]); - } - t[i] = s; } + + auto check = [](const unordered_set & a, const unordered_set & b) { + for (int x : a) { + if (!b.contains(x)) { + return false; + } + } + return true; + }; + vector ans; for (int i = 0; i < n; ++i) { bool ok = true; - for (int j = 0; j < n; ++j) { - if (i == j) continue; - if (check(t[i], t[j])) { + for (int j = 0; j < n && ok; ++j) { + if (i != j && check(nums[i], nums[j])) { ok = false; - break; } } if (ok) { ans.push_back(i); } } - return ans; - } - bool check(unordered_set & nums1, unordered_set & nums2) { - for (int v : nums1) { - if (!nums2.count(v)) { - return false; - } - } - return true; + return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.go b/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.go index 03a26d384ca86..3f680e2545d4d 100644 --- a/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.go +++ b/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.go @@ -1,24 +1,23 @@ -func peopleIndexes(favoriteCompanies [][]string) []int { - d := map[string]int{} - idx, n := 0, len(favoriteCompanies) - t := make([]map[int]bool, n) - for i, v := range favoriteCompanies { - for _, c := range v { - if _, ok := d[c]; !ok { - d[c] = idx +func peopleIndexes(favoriteCompanies [][]string) (ans []int) { + n := len(favoriteCompanies) + d := make(map[string]int) + idx := 0 + nums := make([]map[int]struct{}, n) + + for i := 0; i < n; i++ { + nums[i] = make(map[int]struct{}) + for _, s := range favoriteCompanies[i] { + if _, ok := d[s]; !ok { + d[s] = idx idx++ } + nums[i][d[s]] = struct{}{} } - s := map[int]bool{} - for _, c := range v { - s[d[c]] = true - } - t[i] = s } - ans := []int{} - check := func(nums1, nums2 map[int]bool) bool { - for v, _ := range nums1 { - if _, ok := nums2[v]; !ok { + + check := func(a, b map[int]struct{}) bool { + for x := range a { + if _, ok := b[x]; !ok { return false } } @@ -26,18 +25,15 @@ func peopleIndexes(favoriteCompanies [][]string) []int { } for i := 0; i < n; i++ { ok := true - for j := 0; j < n; j++ { - if i == j { - continue - } - if check(t[i], t[j]) { + for j := 0; j < n && ok; j++ { + if i != j && check(nums[i], nums[j]) { ok = false - break } } if ok { ans = append(ans, i) } } - return ans -} \ No newline at end of file + + return +} diff --git a/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.java b/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.java index 8b718f8bb0631..c81d5aef30feb 100644 --- a/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.java +++ b/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.java @@ -1,31 +1,25 @@ class Solution { public List peopleIndexes(List > favoriteCompanies) { + int n = favoriteCompanies.size(); Map
d = new HashMap<>(); int idx = 0; - int n = favoriteCompanies.size(); - Set [] t = new Set[n]; + Set [] nums = new Set[n]; + Arrays.setAll(nums, i -> new HashSet<>()); for (int i = 0; i < n; ++i) { - var v = favoriteCompanies.get(i); - for (var c : v) { - if (!d.containsKey(c)) { - d.put(c, idx++); + var ss = favoriteCompanies.get(i); + for (var s : ss) { + if (!d.containsKey(s)) { + d.put(s, idx++); } + nums[i].add(d.get(s)); } - Set s = new HashSet<>(); - for (var c : v) { - s.add(d.get(c)); - } - t[i] = s; } List ans = new ArrayList<>(); for (int i = 0; i < n; ++i) { boolean ok = true; - for (int j = 0; j < n; ++j) { - if (i != j) { - if (t[j].containsAll(t[i])) { - ok = false; - break; - } + for (int j = 0; j < n && ok; ++j) { + if (i != j && nums[j].containsAll(nums[i])) { + ok = false; } } if (ok) { @@ -34,4 +28,4 @@ public List peopleIndexes(List > favoriteCompanies) { } return ans; } -} \ No newline at end of file +} diff --git a/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.py b/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.py index 33022ba4cb3c1..3b53f8f92314b 100644 --- a/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.py +++ b/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.py @@ -1,23 +1,17 @@ class Solution: def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]: - d = {} idx = 0 - t = [] - for v in favoriteCompanies: - for c in v: - if c not in d: - d[c] = idx + d = {} + n = len(favoriteCompanies) + nums = [set() for _ in range(n)] + for i, ss in enumerate(favoriteCompanies): + for s in ss: + if s not in d: + d[s] = idx idx += 1 - t.append({d[c] for c in v}) + nums[i].add(d[s]) ans = [] - for i, nums1 in enumerate(t): - ok = True - for j, nums2 in enumerate(t): - if i == j: - continue - if not (nums1 - nums2): - ok = False - break - if ok: + for i in range(n): + if not any(i != j and (nums[i] & nums[j]) == nums[i] for j in range(n)): ans.append(i) return ans diff --git a/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.ts b/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.ts new file mode 100644 index 0000000000000..7bd4171f71cc5 --- /dev/null +++ b/solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/Solution.ts @@ -0,0 +1,39 @@ +function peopleIndexes(favoriteCompanies: string[][]): number[] { + const n = favoriteCompanies.length; + const d: Map
= new Map(); + let idx = 0; + const nums: Set [] = Array.from({ length: n }, () => new Set ()); + + for (let i = 0; i < n; i++) { + for (const s of favoriteCompanies[i]) { + if (!d.has(s)) { + d.set(s, idx++); + } + nums[i].add(d.get(s)!); + } + } + + const check = (a: Set , b: Set ): boolean => { + for (const x of a) { + if (!b.has(x)) { + return false; + } + } + return true; + }; + + const ans: number[] = []; + for (let i = 0; i < n; i++) { + let ok = true; + for (let j = 0; j < n && ok; j++) { + if (i !== j && check(nums[i], nums[j])) { + ok = false; + } + } + if (ok) { + ans.push(i); + } + } + + return ans; +}