-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1258-synonymous-sentences.js
More file actions
69 lines (58 loc) · 1.71 KB
/
1258-synonymous-sentences.js
File metadata and controls
69 lines (58 loc) · 1.71 KB
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
/**
* 1258. Synonymous Sentences
* https://leetcode.com/problems/synonymous-sentences/
* Difficulty: Medium
*
* You are given a list of equivalent string pairs synonyms where synonyms[i] = [si, ti]
* indicates that si and ti are equivalent strings. You are also given a sentence text.
*
* Return all possible synonymous sentences sorted lexicographically.
*/
/**
* @param {string[][]} synonyms
* @param {string} text
* @return {string[]}
*/
var generateSentences = function(synonyms, text) {
const graph = new Map();
for (const [word1, word2] of synonyms) {
if (!graph.has(word1)) graph.set(word1, []);
if (!graph.has(word2)) graph.set(word2, []);
graph.get(word1).push(word2);
graph.get(word2).push(word1);
}
const words = text.split(' ');
const allCombinations = [];
backtrack(0, []);
return allCombinations.sort();
function findSynonyms(word) {
if (!graph.has(word)) return [word];
const visited = new Set();
const queue = [word];
const synonymGroup = [];
while (queue.length > 0) {
const current = queue.shift();
if (visited.has(current)) continue;
visited.add(current);
synonymGroup.push(current);
for (const neighbor of graph.get(current)) {
if (!visited.has(neighbor)) {
queue.push(neighbor);
}
}
}
return synonymGroup.sort();
}
function backtrack(index, currentSentence) {
if (index === words.length) {
allCombinations.push(currentSentence.join(' '));
return;
}
const synonyms = findSynonyms(words[index]);
for (const synonym of synonyms) {
currentSentence.push(synonym);
backtrack(index + 1, currentSentence);
currentSentence.pop();
}
}
};