forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1452.java
72 lines (70 loc) · 2.62 KB
/
_1452.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class _1452 {
public static class Solution1 {
public List<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
TreeMap<String, Integer> map = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int diffLength = o1.length() - o2.length();
if (diffLength != 0) {
return diffLength;
}
return o1.compareTo(o2);
}
});
Map<String, Set<String>> setMap = new HashMap<>();
for (int i = 0; i < favoriteCompanies.size(); i++) {
List<String> list = favoriteCompanies.get(i);
Collections.sort(list);
StringBuilder sb = new StringBuilder();
Set<String> set = new HashSet<>();
for (String str : list) {
sb.append(str);
set.add(str);
}
map.put(sb.toString(), i);
setMap.put(sb.toString(), set);
}
List<String> keys = new ArrayList<>();
for (String key : map.keySet()) {
keys.add(key);
}
List<Integer> result = new ArrayList<>();
for (int i = 0; i < keys.size(); i++) {
boolean goodCandidate = true;
for (int j = i + 1; j < keys.size(); j++) {
if (keys.get(j).contains(keys.get(i))) {
goodCandidate = false;
break;
}
}
if (goodCandidate) {
Set<String> smallerSet = setMap.get(keys.get(i));
for (int j = 0; j < keys.size(); j++) {
if (j != i) {
Set<String> biggerSet = setMap.get(keys.get(j));
if (biggerSet.containsAll(smallerSet)) {
goodCandidate = false;
break;
}
}
}
}
if (goodCandidate) {
result.add(map.get(keys.get(i)));
}
}
Collections.sort(result);
return result;
}
}
}