- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1452.java
74 lines (72 loc) · 2.82 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
73
74
packagecom.fishercoder.solutions.secondthousand;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.Comparator;
importjava.util.HashMap;
importjava.util.HashSet;
importjava.util.List;
importjava.util.Map;
importjava.util.Set;
importjava.util.TreeMap;
publicclass_1452 {
publicstaticclassSolution1 {
publicList<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
TreeMap<String, Integer> map =
newTreeMap<>(
newComparator<String>() {
@Override
publicintcompare(Stringo1, Stringo2) {
intdiffLength = o1.length() - o2.length();
if (diffLength != 0) {
returndiffLength;
}
returno1.compareTo(o2);
}
});
Map<String, Set<String>> setMap = newHashMap<>();
for (inti = 0; i < favoriteCompanies.size(); i++) {
List<String> list = favoriteCompanies.get(i);
Collections.sort(list);
StringBuildersb = newStringBuilder();
Set<String> set = newHashSet<>();
for (Stringstr : list) {
sb.append(str);
set.add(str);
}
map.put(sb.toString(), i);
setMap.put(sb.toString(), set);
}
List<String> keys = newArrayList<>();
for (Stringkey : map.keySet()) {
keys.add(key);
}
List<Integer> result = newArrayList<>();
for (inti = 0; i < keys.size(); i++) {
booleangoodCandidate = true;
for (intj = 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 (intj = 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);
returnresult;
}
}
}