Skip to content

Commit e21b17f

Browse files
committed
feat: add solutions to lc problem: No.0734. Sentence Similarity
1 parent 3e85e3d commit e21b17f

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed

solution/0700-0799/0734.Sentence Similarity/README.md

+30-3
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,54 @@
3131

3232
<p>&nbsp;</p>
3333

34-
3534
## 解法
3635

3736
<!-- 这里可写通用的实现逻辑 -->
3837

38+
“哈希表”实现。
39+
3940
<!-- tabs:start -->
4041

4142
### **Python3**
4243

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

4546
```python
46-
47+
class Solution:
48+
def areSentencesSimilar(self, sentence1: List[str], sentence2: List[str], similarPairs: List[List[str]]) -> bool:
49+
if len(sentence1) != len(sentence2):
50+
return False
51+
pairs = {(word1, word2) for word1, word2 in similarPairs}
52+
for i in range(len(sentence1)):
53+
similar = (sentence1[i], sentence2[i]) in pairs or (sentence2[i], sentence1[i]) in pairs or sentence1[i] == sentence2[i]
54+
if not similar:
55+
return False
56+
return True
4757
```
4858

4959
### **Java**
5060

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

5363
```java
54-
64+
class Solution {
65+
public boolean areSentencesSimilar(String[] sentence1, String[] sentence2, List<List<String>> similarPairs) {
66+
if (sentence1.length != sentence2.length) {
67+
return false;
68+
}
69+
Set<String> pairs = new HashSet<>();
70+
for (List<String> pair : similarPairs) {
71+
pairs.add(pair.get(0) + "." + pair.get(1));
72+
}
73+
for (int i = 0; i < sentence1.length; ++i) {
74+
boolean similar = pairs.contains(sentence1[i] + "." + sentence2[i]) || pairs.contains(sentence2[i] + "." + sentence1[i]) || sentence1[i].equals(sentence2[i]);
75+
if (!similar) {
76+
return false;
77+
}
78+
}
79+
return true;
80+
}
81+
}
5582
```
5683

5784
### **...**

solution/0700-0799/0734.Sentence Similarity/README_EN.md

+28-3
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,46 @@
5858
<li>All the pairs <code>(x<sub>i</sub>,<sub>&nbsp;</sub>y<sub>i</sub>)</code> are <strong>distinct</strong>.</li>
5959
</ul>
6060

61-
6261
## Solutions
6362

6463
<!-- tabs:start -->
6564

6665
### **Python3**
6766

6867
```python
69-
68+
class Solution:
69+
def areSentencesSimilar(self, sentence1: List[str], sentence2: List[str], similarPairs: List[List[str]]) -> bool:
70+
if len(sentence1) != len(sentence2):
71+
return False
72+
pairs = {(word1, word2) for word1, word2 in similarPairs}
73+
for i in range(len(sentence1)):
74+
similar = (sentence1[i], sentence2[i]) in pairs or (sentence2[i], sentence1[i]) in pairs or sentence1[i] == sentence2[i]
75+
if not similar:
76+
return False
77+
return True
7078
```
7179

7280
### **Java**
7381

7482
```java
75-
83+
class Solution {
84+
public boolean areSentencesSimilar(String[] sentence1, String[] sentence2, List<List<String>> similarPairs) {
85+
if (sentence1.length != sentence2.length) {
86+
return false;
87+
}
88+
Set<String> pairs = new HashSet<>();
89+
for (List<String> pair : similarPairs) {
90+
pairs.add(pair.get(0) + "." + pair.get(1));
91+
}
92+
for (int i = 0; i < sentence1.length; ++i) {
93+
boolean similar = pairs.contains(sentence1[i] + "." + sentence2[i]) || pairs.contains(sentence2[i] + "." + sentence1[i]) || sentence1[i].equals(sentence2[i]);
94+
if (!similar) {
95+
return false;
96+
}
97+
}
98+
return true;
99+
}
100+
}
76101
```
77102

78103
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public boolean areSentencesSimilar(String[] sentence1, String[] sentence2, List<List<String>> similarPairs) {
3+
if (sentence1.length != sentence2.length) {
4+
return false;
5+
}
6+
Set<String> pairs = new HashSet<>();
7+
for (List<String> pair : similarPairs) {
8+
pairs.add(pair.get(0) + "." + pair.get(1));
9+
}
10+
for (int i = 0; i < sentence1.length; ++i) {
11+
boolean similar = pairs.contains(sentence1[i] + "." + sentence2[i]) || pairs.contains(sentence2[i] + "." + sentence1[i]) || sentence1[i].equals(sentence2[i]);
12+
if (!similar) {
13+
return false;
14+
}
15+
}
16+
return true;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def areSentencesSimilar(self, sentence1: List[str], sentence2: List[str], similarPairs: List[List[str]]) -> bool:
3+
if len(sentence1) != len(sentence2):
4+
return False
5+
pairs = {(word1, word2) for word1, word2 in similarPairs}
6+
for i in range(len(sentence1)):
7+
similar = (sentence1[i], sentence2[i]) in pairs or (sentence2[i], sentence1[i]) in pairs or sentence1[i] == sentence2[i]
8+
if not similar:
9+
return False
10+
return True

0 commit comments

Comments
 (0)