-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.java
66 lines (64 loc) · 2.25 KB
/
Solution.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
class Solution {
private Map<String, String> p;
public List<String> generateSentences(List<List<String>> synonyms, String text) {
p = new HashMap<>();
for (List<String> item : synonyms) {
p.put(item.get(0), item.get(0));
p.put(item.get(1), item.get(1));
}
for (List<String> item : synonyms) {
p.put(find(item.get(0)), find(item.get(1)));
}
Map<String, Set<String>> s = new HashMap<>();
for (List<String> item : synonyms) {
String a = find(item.get(0)), b = find(item.get(1));
s.computeIfAbsent(a, k -> new HashSet<>()).add(item.get(0));
s.computeIfAbsent(b, k -> new HashSet<>()).add(item.get(1));
}
List<List<String>> all = new ArrayList<>();
for (String word : text.split(" ")) {
if (!p.containsKey(word)) {
if (all.isEmpty()) {
List<String> t = new ArrayList<>();
t.add(word);
all.add(t);
} else {
for (List<String> a : all) {
a.add(word);
}
}
} else {
Set<String> words = s.get(find(word));
if (all.isEmpty()) {
for (String b : words) {
List<String> t = new ArrayList<>();
t.add(b);
all.add(t);
}
} else {
List<List<String>> t = new ArrayList<>();
for (List<String> a : all) {
for (String b : words) {
List<String> c = new ArrayList<>(a);
c.add(b);
t.add(c);
}
}
all = t;
}
}
}
List<String> res = new ArrayList<>();
for (List<String> item : all) {
res.add(String.join(" ", item));
}
Collections.sort(res);
return res;
}
private String find(String x) {
if (!Objects.equals(p.get(x), x)) {
p.put(x, find(p.get(x)));
}
return p.get(x);
}
}