forked from ex01tus/leetcode-grind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynonymousSentences.java
More file actions
139 lines (114 loc) · 4.41 KB
/
SynonymousSentences.java
File metadata and controls
139 lines (114 loc) · 4.41 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package graph;
import java.util.*;
/**
* Description: https://leetcode.com/problems/synonymous-sentences
* Difficulty: Medium
*/
public class SynonymousSentences {
private final Map<String, String> parents = new HashMap<>();
private final Map<String, Integer> ranks = new HashMap<>();
/**
* Time complexity: O(?)
* Space complexity: O(?)
*/
public List<String> generateSentencesViaUnionFind(List<List<String>> synonyms, String text) {
Set<String> wordsWithSynonyms = new HashSet<>();
for (List<String> pair : synonyms) {
unite(pair.get(0), pair.get(1));
wordsWithSynonyms.add(pair.get(0));
wordsWithSynonyms.add(pair.get(1));
}
String[] words = text.split(" ");
Map<String, Set<String>> synonymGroups = buildSynonymGroups(words, wordsWithSynonyms);
List<String> result = new ArrayList<>();
backtrack(words, synonymGroups, 0, new String[words.length], result);
Collections.sort(result);
return result;
}
private String findParent(String word) {
parents.putIfAbsent(word, word);
while (!word.equals(parents.get(word))) {
parents.put(word, parents.get(parents.get(word)));
word = parents.get(word);
}
return word;
}
private void unite(String word1, String word2) {
String parent1 = findParent(word1);
String parent2 = findParent(word2);
if (parent1.equals(parent2)) return;
int rank1 = ranks.getOrDefault(parent1, 1);
int rank2 = ranks.getOrDefault(parent2, 1);
if (rank1 > rank2) {
parents.put(parent2, parent1);
ranks.put(parent1, rank1 + rank2);
} else {
parents.put(parent1, parent2);
ranks.put(parent2, rank2 + rank1);
}
}
private boolean areConnected(String word1, String word2) {
String parent1 = findParent(word1);
String parent2 = findParent(word2);
return parent1.equals(parent2);
}
private Map<String, Set<String>> buildSynonymGroups(String[] words, Set<String> wordsWithSynonyms) {
Map<String, Set<String>> wordGroups = new HashMap<>();
for (String word : words) {
for (String unique : wordsWithSynonyms) {
if (areConnected(word, unique)) {
wordGroups.computeIfAbsent(word, __ -> new HashSet<>()).add(unique);
}
}
}
return wordGroups;
}
private void backtrack(
String[] text,
Map<String, Set<String>> synonyms,
int current,
String[] currentSentence,
List<String> result) {
if (current == text.length) {
result.add(String.join(" ", currentSentence));
return;
}
for (String synonym : synonyms.getOrDefault(text[current], Set.of(text[current]))) {
currentSentence[current] = synonym;
backtrack(text, synonyms, current + 1, currentSentence, result);
}
}
/**
* Time complexity: O(?)
* Space complexity: O(?)
*/
public List<String> generateSentencesViaBFS(List<List<String>> synonyms, String text) {
Map<String, List<String>> adjList = buildAdjList(synonyms);
Set<String> result = new TreeSet<>();
Queue<String> planned = new LinkedList<>();
planned.offer(text);
while (!planned.isEmpty()) {
String current = planned.poll();
result.add(current);
String[] words = current.split(" ");
for (int i = 0; i < words.length; i++) {
for (String synonym : adjList.getOrDefault(words[i], List.of())) {
words[i] = synonym;
String newSentence = String.join(" ", words);
if (!result.contains(newSentence)) {
planned.offer(newSentence);
}
}
}
}
return new ArrayList<>(result);
}
private Map<String, List<String>> buildAdjList(List<List<String>> synonyms) {
Map<String, List<String>> adjList = new HashMap<>();
for (List<String> pair : synonyms) {
adjList.computeIfAbsent(pair.get(0), __ -> new ArrayList<>()).add(pair.get(1));
adjList.computeIfAbsent(pair.get(1), __ -> new ArrayList<>()).add(pair.get(0));
}
return adjList;
}
}