-
Notifications
You must be signed in to change notification settings - Fork 43
/
SentenceSimilarity2.java
209 lines (191 loc) · 7.3 KB
/
SentenceSimilarity2.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package LeetCodeJava.DFS;
// https://leetcode.com/problems/sentence-similarity-ii/editorial/
// https://leetcode.ca/all/737.html
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* 737. Sentence Similarity II
* Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar.
*
* For example, words1 = ["great", "acting", "skills"] and words2 = ["fine", "drama", "talent"] are similar, if the similar word pairs are pairs = [["great", "good"], ["fine", "good"], ["acting","drama"], ["skills","talent"]].
*
* Note that the similarity relation is transitive. For example, if "great" and "good" are similar, and "fine" and "good" are similar, then "great" and "fine" are similar.
*
* Similarity is also symmetric. For example, "great" and "fine" being similar is the same as "fine" and "great" being similar.
*
* Also, a word is always similar with itself. For example, the sentences words1 = ["great"], words2 = ["great"], pairs = [] are similar, even though there are no specified similar word pairs.
*
* Finally, sentences can only be similar if they have the same number of words. So a sentence like words1 = ["great"] can never be similar to words2 = ["doubleplus","good"].
*
* Note:
*
* The length of words1 and words2 will not exceed 1000.
* The length of pairs will not exceed 2000.
* The length of each pairs[i] will be 2.
* The length of each words[i] and pairs[i][j] will be in the range [1, 20].
*
*/
public class SentenceSimilarity2 {
// V0
// TODO : implement
// public boolean areSentencesSimilarTwo(){}
// V1
// https://leetcode.ca/2017-12-06-737-Sentence-Similarity-II/
private int[] p;
public boolean areSentencesSimilarTwo_1(
String[] sentence1, String[] sentence2, List<List<String>> similarPairs) {
if (sentence1.length != sentence2.length) {
return false;
}
int n = similarPairs.size();
/**
*
* Explain p = new int[n << 1]; ?
*
* The p variable is being initialized as an integer array with a size of n << 1. Here’s a breakdown of the key elements:
*
* 1. int[n << 1]:
* • The n << 1 operation is a bitwise left shift. This means that the value of n is shifted left by 1 bit. In binary terms, this effectively multiplies n by 2.
* • For example, if n = 5, its binary representation is 101. Shifting it left by one position (n << 1) gives 1010, which is 10 in decimal. Therefore, n << 1 results in 2 * n.
* 2. p = new int[n << 1]:
* • This allocates an integer array of size 2 * n. For instance, if n = 5, the size of the array p would be 10.
*
*
* Purpose:
*
* This operation is often used when you need to allocate extra space, such as in algorithms involving data structures like stacks, arrays, or graphs. Doubling the size can be beneficial for dynamic resizing or managing additional elements.
*
*
*
*
* Yes,
* you can replace the bitwise operation n << 1
* with a simple multiplication by 2.
* The left shift operation n << 1 is equivalent to multiplying n by 2
*/
//p = new int[n << 1];
p = new int[n * 2];
for (int i = 0; i < p.length; ++i) {
p[i] = i;
}
Map<String, Integer> words = new HashMap<>();
int idx = 0;
for (List<String> e : similarPairs) {
String a = e.get(0), b = e.get(1);
if (!words.containsKey(a)) {
words.put(a, idx++);
}
if (!words.containsKey(b)) {
words.put(b, idx++);
}
p[find(words.get(a))] = find(words.get(b));
}
for (int i = 0; i < sentence1.length; ++i) {
if (Objects.equals(sentence1[i], sentence2[i])) {
continue;
}
if (!words.containsKey(sentence1[i]) || !words.containsKey(sentence2[i])
|| find(words.get(sentence1[i])) != find(words.get(sentence2[i]))) {
return false;
}
}
return true;
}
private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
// V2
// V3
// IDEA : Disjoint-set
// https://blog.csdn.net/susuxuezhang/article/details/100127908
// https://www.cnblogs.com/grandyang/p/8053934.html
// V4-1
// IDEA : DFS
// https://zxi.mytechroad.com/blog/hashtable/leetcode-737-sentence-similarity-ii/
// C++
// class Solution {
// public:
// bool areSentencesSimilarTwo(vector<string>& words1, vector<string>& words2, vector<pair<string, string>>& pairs) {
// if (words1.size() != words2.size()) return false;
//
// g_.clear();
//
// for (const auto& p : pairs) {
// g_[p.first].insert(p.second);
// g_[p.second].insert(p.first);
// }
//
// unordered_set<string> visited;
//
// for (int i = 0; i < words1.size(); ++i) {
// visited.clear();
// if (!dfs(words1[i], words2[i], visited)) return false;
// }
//
// return true;
// }
// private:
// bool dfs(const string& src, const string& dst, unordered_set<string>& visited) {
// if (src == dst) return true;
// visited.insert(src);
// for (const auto& next : g_[src]) {
// if (visited.count(next)) continue;
// if (dfs(next, dst, visited)) return true;
// }
// return false;
// }
// unordered_map<string, unordered_set<string>> g_;
// };
// V4-2
// IDEA : Union Find
// https://zxi.mytechroad.com/blog/hashtable/leetcode-737-sentence-similarity-ii/
// C++
// class UnionFindSet {
// public:
// bool Union(const string& word1, const string& word2) {
// const string& p1 = Find(word1, true);
// const string& p2 = Find(word2, true);
// if (p1 == p2) return false;
// parents_[p1] = p2;
// return true;
// }
//
// const string& Find(const string& word, bool create = false) {
// if (!parents_.count(word)) {
// if (!create) return word;
// return parents_[word] = word;
// }
//
// string w = word;
// while (w != parents_[w]) {
// parents_[w] = parents_[parents_[w]];
// w = parents_[w];
// }
//
// return parents_[w];
// }
// private:
// unordered_map<string, string> parents_;
// };
//
// class Solution {
// public:
// bool areSentencesSimilarTwo(vector<string>& words1, vector<string>& words2, vector<pair<string, string>>& pairs) {
// if (words1.size() != words2.size()) return false;
//
// UnionFindSet s;
// for (const auto& pair : pairs)
// s.Union(pair.first, pair.second);
//
// for (int i = 0; i < words1.size(); ++i)
// if (s.Find(words1[i]) != s.Find(words2[i])) return false;
//
// return true;
// }
// };
}