Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9dafaa5

Browse files
committed
feat: add solutions to lc problem: No.0737.Sentence Similarity II
1 parent cf88375 commit 9dafaa5

File tree

6 files changed

+440
-3
lines changed

6 files changed

+440
-3
lines changed

‎solution/0700-0799/0737.Sentence Similarity II/README.md‎

Lines changed: 173 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,198 @@
2727
<li>每个&nbsp;<code>words[i]</code>&nbsp;和&nbsp;<code>pairs[i][j]</code>&nbsp;的长度范围为&nbsp;<code>[1, 20]</code>。</li>
2828
</ul>
2929

30-
3130
## 解法
3231

3332
<!-- 这里可写通用的实现逻辑 -->
3433

34+
并查集。
35+
36+
模板 1——朴素并查集:
37+
38+
```python
39+
# 初始化,p存储每个点的祖宗节点
40+
p = [i for i in range(n)]
41+
# 返回x的祖宗节点
42+
def find(x):
43+
if p[x] != x:
44+
# 路径压缩
45+
p[x] = find(p[x])
46+
return p[x]
47+
# 合并a和b所在的两个集合
48+
p[find(a)] = find(b)
49+
```
50+
51+
模板 2——维护 size 的并查集:
52+
53+
```python
54+
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
55+
p = [i for i in range(n)]
56+
size = [1] * n
57+
# 返回x的祖宗节点
58+
def find(x):
59+
if p[x] != x:
60+
# 路径压缩
61+
p[x] = find(p[x])
62+
return p[x]
63+
# 合并a和b所在的两个集合
64+
size[find(b)] += size[find(a)]
65+
p[find(a)] = find(b)
66+
```
67+
68+
模板 3——维护到祖宗节点距离的并查集:
69+
70+
```python
71+
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
72+
p = [i for i in range(n)]
73+
d = [0] * n
74+
# 返回x的祖宗节点
75+
def find(x):
76+
if p[x] != x:
77+
t = find(p[x])
78+
d[x] += d[p[x]]
79+
p[x] = t
80+
return p[x]
81+
# 合并a和b所在的两个集合
82+
p[find(a)] = find(b)
83+
d[find(a)] = dinstance
84+
```
85+
86+
对于本题,将相似对的所有单词转换为下标,然后套用并查集模板,将相似对合并。
87+
88+
接着遍历 `sentence1`, `sentence2`,若对应的单词相同,直接 continue;若对应的单词不在相似对单词中,或者两单词不在同一个集合中,直接返回 false。否则遍历结束返回 true。
89+
3590
<!-- tabs:start -->
3691

3792
### **Python3**
3893

3994
<!-- 这里可写当前语言的特殊实现逻辑 -->
4095

4196
```python
42-
97+
class Solution:
98+
def areSentencesSimilarTwo(self, sentence1: List[str], sentence2: List[str], similarPairs: List[List[str]]) -> bool:
99+
if len(sentence1) != len(sentence2):
100+
return False
101+
n = len(similarPairs)
102+
p = list(range(n << 1))
103+
104+
def find(x):
105+
if p[x] != x:
106+
p[x] = find(p[x])
107+
return p[x]
108+
109+
words = {}
110+
idx = 0
111+
for a, b in similarPairs:
112+
if a not in words:
113+
words[a] = idx
114+
idx += 1
115+
if b not in words:
116+
words[b] = idx
117+
idx += 1
118+
p[find(words[a])] = find(words[b])
119+
120+
for i in range(len(sentence1)):
121+
if sentence1[i] == sentence2[i]:
122+
continue
123+
if sentence1[i] not in words or sentence2[i] not in words or find(words[sentence1[i]]) != find(words[sentence2[i]]):
124+
return False
125+
return True
43126
```
44127

45128
### **Java**
46129

47130
<!-- 这里可写当前语言的特殊实现逻辑 -->
48131

49132
```java
133+
class Solution {
134+
private int[] p;
135+
136+
public boolean areSentencesSimilarTwo(String[] sentence1, String[] sentence2, List<List<String>> similarPairs) {
137+
if (sentence1.length != sentence2.length) {
138+
return false;
139+
}
140+
int n = similarPairs.size();
141+
p = new int[n << 1];
142+
for (int i = 0; i < p.length; ++i) {
143+
p[i] = i;
144+
}
145+
Map<String, Integer> words = new HashMap<>();
146+
int idx = 0;
147+
for (List<String> e : similarPairs) {
148+
String a = e.get(0), b = e.get(1);
149+
if (!words.containsKey(a)) {
150+
words.put(a, idx++);
151+
}
152+
if (!words.containsKey(b)) {
153+
words.put(b, idx++);
154+
}
155+
p[find(words.get(a))] = find(words.get(b));
156+
}
157+
for (int i = 0; i < sentence1.length; ++i) {
158+
if (Objects.equals(sentence1[i], sentence2[i])) {
159+
continue;
160+
}
161+
if (!words.containsKey(sentence1[i]) || !words.containsKey(sentence2[i]) || find(words.get(sentence1[i])) != find(words.get(sentence2[i]))) {
162+
return false;
163+
}
164+
}
165+
return true;
166+
}
167+
168+
private int find(int x) {
169+
if (p[x] != x) {
170+
p[x] = find(p[x]);
171+
}
172+
return p[x];
173+
}
174+
}
175+
```
50176

177+
### **Go**
178+
179+
```go
180+
var p []int
181+
182+
func areSentencesSimilarTwo(sentence1 []string, sentence2 []string, similarPairs [][]string) bool {
183+
if len(sentence1) != len(sentence2) {
184+
return false
185+
}
186+
n := len(similarPairs)
187+
p = make([]int, (n<<1)+10)
188+
for i := 0; i < len(p); i++ {
189+
p[i] = i
190+
}
191+
words := make(map[string]int)
192+
idx := 1
193+
for _, e := range similarPairs {
194+
a, b := e[0], e[1]
195+
if words[a] == 0 {
196+
words[a] = idx
197+
idx++
198+
}
199+
if words[b] == 0 {
200+
words[b] = idx
201+
idx++
202+
}
203+
p[find(words[a])] = find(words[b])
204+
}
205+
for i := 0; i < len(sentence1); i++ {
206+
if sentence1[i] == sentence2[i] {
207+
continue
208+
}
209+
if words[sentence1[i]] == 0 || words[sentence2[i]] == 0 || find(words[sentence1[i]]) != find(words[sentence2[i]]) {
210+
return false
211+
}
212+
}
213+
return true
214+
}
215+
216+
func find(x int) int {
217+
if p[x] != x {
218+
p[x] = find(p[x])
219+
}
220+
return p[x]
221+
}
51222
```
52223

53224
### **...**

‎solution/0700-0799/0737.Sentence Similarity II/README_EN.md‎

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,129 @@
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def areSentencesSimilarTwo(self, sentence1: List[str], sentence2: List[str], similarPairs: List[List[str]]) -> bool:
57+
if len(sentence1) != len(sentence2):
58+
return False
59+
n = len(similarPairs)
60+
p = list(range(n << 1))
61+
62+
def find(x):
63+
if p[x] != x:
64+
p[x] = find(p[x])
65+
return p[x]
66+
67+
words = {}
68+
idx = 0
69+
for a, b in similarPairs:
70+
if a not in words:
71+
words[a] = idx
72+
idx += 1
73+
if b not in words:
74+
words[b] = idx
75+
idx += 1
76+
p[find(words[a])] = find(words[b])
77+
78+
for i in range(len(sentence1)):
79+
if sentence1[i] == sentence2[i]:
80+
continue
81+
if sentence1[i] not in words or sentence2[i] not in words or find(words[sentence1[i]]) != find(words[sentence2[i]]):
82+
return False
83+
return True
5684
```
5785

5886
### **Java**
5987

6088
```java
89+
class Solution {
90+
private int[] p;
91+
92+
public boolean areSentencesSimilarTwo(String[] sentence1, String[] sentence2, List<List<String>> similarPairs) {
93+
if (sentence1.length != sentence2.length) {
94+
return false;
95+
}
96+
int n = similarPairs.size();
97+
p = new int[n << 1];
98+
for (int i = 0; i < p.length; ++i) {
99+
p[i] = i;
100+
}
101+
Map<String, Integer> words = new HashMap<>();
102+
int idx = 0;
103+
for (List<String> e : similarPairs) {
104+
String a = e.get(0), b = e.get(1);
105+
if (!words.containsKey(a)) {
106+
words.put(a, idx++);
107+
}
108+
if (!words.containsKey(b)) {
109+
words.put(b, idx++);
110+
}
111+
p[find(words.get(a))] = find(words.get(b));
112+
}
113+
for (int i = 0; i < sentence1.length; ++i) {
114+
if (Objects.equals(sentence1[i], sentence2[i])) {
115+
continue;
116+
}
117+
if (!words.containsKey(sentence1[i]) || !words.containsKey(sentence2[i]) || find(words.get(sentence1[i])) != find(words.get(sentence2[i]))) {
118+
return false;
119+
}
120+
}
121+
return true;
122+
}
123+
124+
private int find(int x) {
125+
if (p[x] != x) {
126+
p[x] = find(p[x]);
127+
}
128+
return p[x];
129+
}
130+
}
131+
```
61132

133+
### **Go**
134+
135+
```go
136+
var p []int
137+
138+
func areSentencesSimilarTwo(sentence1 []string, sentence2 []string, similarPairs [][]string) bool {
139+
if len(sentence1) != len(sentence2) {
140+
return false
141+
}
142+
n := len(similarPairs)
143+
p = make([]int, (n<<1)+10)
144+
for i := 0; i < len(p); i++ {
145+
p[i] = i
146+
}
147+
words := make(map[string]int)
148+
idx := 1
149+
for _, e := range similarPairs {
150+
a, b := e[0], e[1]
151+
if words[a] == 0 {
152+
words[a] = idx
153+
idx++
154+
}
155+
if words[b] == 0 {
156+
words[b] = idx
157+
idx++
158+
}
159+
p[find(words[a])] = find(words[b])
160+
}
161+
for i := 0; i < len(sentence1); i++ {
162+
if sentence1[i] == sentence2[i] {
163+
continue
164+
}
165+
if words[sentence1[i]] == 0 || words[sentence2[i]] == 0 || find(words[sentence1[i]]) != find(words[sentence2[i]]) {
166+
return false
167+
}
168+
}
169+
return true
170+
}
171+
172+
func find(x int) int {
173+
if p[x] != x {
174+
p[x] = find(p[x])
175+
}
176+
return p[x]
177+
}
62178
```
63179

64180
### **...**
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
vector<int> p;
4+
bool areSentencesSimilarTwo(vector<string> &sentence1, vector<string> &sentence2, vector<vector<string>> &similarPairs) {
5+
if (sentence1.size() != sentence2.size())
6+
return false;
7+
int n = similarPairs.size();
8+
p.resize(n << 1);
9+
for (int i = 0; i < p.size(); ++i)
10+
p[i] = i;
11+
unordered_map<string, int> words;
12+
int idx = 0;
13+
for (auto e : similarPairs)
14+
{
15+
string a = e[0], b = e[1];
16+
if (!words.count(a))
17+
words[a] = idx++;
18+
if (!words.count(b))
19+
words[b] = idx++;
20+
p[find(words[a])] = find(words[b]);
21+
}
22+
for (int i = 0; i < sentence1.size(); ++i)
23+
{
24+
if (sentence1[i] == sentence2[i])
25+
continue;
26+
if (!words.count(sentence1[i]) || !words.count(sentence2[i]) || find(words[sentence1[i]]) != find(words[sentence2[i]]))
27+
return false;
28+
}
29+
return true;
30+
}
31+
32+
int find(int x) {
33+
if (p[x] != x)
34+
p[x] = find(p[x]);
35+
return p[x];
36+
}
37+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /