-
Notifications
You must be signed in to change notification settings - Fork 481
/
1258.js
51 lines (48 loc) · 1.24 KB
/
1258.js
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
/**
* @param {string[][]} synonyms
* @param {string} text
* @return {string[]}
*/
var generateSentences = function(synonyms, text) {
let parent = new Map(), data = new Map();
let find = function(x) {
if (!parent.has(x)) parent.set(x, x);
if (x != parent.get(x)) {
parent.set(x, find(parent.get(x)));
}
return parent.get(x);
}
for (let it of synonyms) {
let x = find(it[0]), y = find(it[1]);
if (x != y) parent.set(x, y);
}
for (let k of parent.keys()) {
let fk = find(k);
if (!data.has(fk)) {
data.set(find(k), []);
}
data.get(fk).push(k);
}
let txt = text.split(" "), res = [""];
for (let w of txt) {
let fw = find(w);
if (data.has(fw)) {
let tmp = [];
for (let s of res) {
for (let v of data.get(fw)) {
tmp.push(s + " " + v);
}
}
res = tmp;
} else {
for (let i = 0; i < res.length; i++) {
res[i] += " " + w;
}
}
}
for (let i = 0; i < res.length; i++) {
res[i] = res[i].substring(1);
}
res.sort();
return res;
};