Skip to content

Commit 45ce2d1

Browse files
committed
feat: update solutions to lc problem: No.0734
No.0734.Sentence Similarity
1 parent 81bb836 commit 45ce2d1

File tree

9 files changed

+142
-43
lines changed

9 files changed

+142
-43
lines changed

solution/0600-0699/0605.Can Place Flowers/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution:
22
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
33
flowerbed = [0] + flowerbed + [0]
44
for i in range(1, len(flowerbed) - 1):
5-
if sum(flowerbed[i - 1: i + 2]) == 0:
5+
if sum(flowerbed[i - 1 : i + 2]) == 0:
66
flowerbed[i] = 1
77
n -= 1
88
return n <= 0

solution/0600-0699/0616.Add Bold Tag in String/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def addBoldTag(self, s: str, words: List[str]) -> str:
4949
break
5050
st, ed = t[j]
5151
if i < st:
52-
ans.append(s[i: st])
52+
ans.append(s[i:st])
5353
ans.append('<b>')
54-
ans.append(s[st: ed + 1])
54+
ans.append(s[st : ed + 1])
5555
ans.append('</b>')
5656
j += 1
5757
i = ed + 1

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

+49-12
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
<!-- 这里可写通用的实现逻辑 -->
6666

67-
哈希表”实现。
67+
**方法一:哈希表**
6868

6969
<!-- tabs:start -->
7070

@@ -77,12 +77,8 @@ class Solution:
7777
def areSentencesSimilar(self, sentence1: List[str], sentence2: List[str], similarPairs: List[List[str]]) -> bool:
7878
if len(sentence1) != len(sentence2):
7979
return False
80-
pairs = {(word1, word2) for word1, word2 in similarPairs}
81-
for i in range(len(sentence1)):
82-
similar = (sentence1[i], sentence2[i]) in pairs or (sentence2[i], sentence1[i]) in pairs or sentence1[i] == sentence2[i]
83-
if not similar:
84-
return False
85-
return True
80+
s = {(a, b) for a, b in similarPairs}
81+
return all(a == b or (a, b) in s or (b, a) in s for a, b in zip(sentence1, sentence2))
8682
```
8783

8884
### **Java**
@@ -95,13 +91,13 @@ class Solution {
9591
if (sentence1.length != sentence2.length) {
9692
return false;
9793
}
98-
Set<String> pairs = new HashSet<>();
99-
for (List<String> pair : similarPairs) {
100-
pairs.add(pair.get(0) + "." + pair.get(1));
94+
Set<String> s = new HashSet<>();
95+
for (List<String> e : similarPairs) {
96+
s.add(e.get(0) + "." + e.get(1));
10197
}
10298
for (int i = 0; i < sentence1.length; ++i) {
103-
boolean similar = pairs.contains(sentence1[i] + "." + sentence2[i]) || pairs.contains(sentence2[i] + "." + sentence1[i]) || sentence1[i].equals(sentence2[i]);
104-
if (!similar) {
99+
String a = sentence1[i], b = sentence2[i];
100+
if (!a.equals(b) && !s.contains(a + "." + b) && !s.contains(b + "." + a)) {
105101
return false;
106102
}
107103
}
@@ -110,6 +106,47 @@ class Solution {
110106
}
111107
```
112108

109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
bool areSentencesSimilar(vector<string>& sentence1, vector<string>& sentence2, vector<vector<string>>& similarPairs) {
115+
int m = sentence1.size(), n = sentence2.size();
116+
if (m != n) return false;
117+
unordered_set<string> s;
118+
for (auto e : similarPairs) s.insert(e[0] + "." + e[1]);
119+
for (int i = 0; i < n; ++i)
120+
{
121+
string a = sentence1[i], b = sentence2[i];
122+
if (a != b && !s.count(a + "." + b) && !s.count(b + "." + a)) return false;
123+
}
124+
return true;
125+
}
126+
};
127+
```
128+
129+
### **Go**
130+
131+
```go
132+
func areSentencesSimilar(sentence1 []string, sentence2 []string, similarPairs [][]string) bool {
133+
if len(sentence1) != len(sentence2) {
134+
return false
135+
}
136+
s := map[string]bool{}
137+
for _, e := range similarPairs {
138+
s[e[0]+"."+e[1]] = true
139+
}
140+
for i, a := range sentence1 {
141+
b := sentence2[i]
142+
if a != b && !s[a+"."+b] && !s[b+"."+a] {
143+
return false
144+
}
145+
}
146+
return true
147+
}
148+
```
149+
113150
### **...**
114151

115152
```

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

+48-11
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,8 @@ class Solution:
6969
def areSentencesSimilar(self, sentence1: List[str], sentence2: List[str], similarPairs: List[List[str]]) -> bool:
7070
if len(sentence1) != len(sentence2):
7171
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
72+
s = {(a, b) for a, b in similarPairs}
73+
return all(a == b or (a, b) in s or (b, a) in s for a, b in zip(sentence1, sentence2))
7874
```
7975

8076
### **Java**
@@ -85,13 +81,13 @@ class Solution {
8581
if (sentence1.length != sentence2.length) {
8682
return false;
8783
}
88-
Set<String> pairs = new HashSet<>();
89-
for (List<String> pair : similarPairs) {
90-
pairs.add(pair.get(0) + "." + pair.get(1));
84+
Set<String> s = new HashSet<>();
85+
for (List<String> e : similarPairs) {
86+
s.add(e.get(0) + "." + e.get(1));
9187
}
9288
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) {
89+
String a = sentence1[i], b = sentence2[i];
90+
if (!a.equals(b) && !s.contains(a + "." + b) && !s.contains(b + "." + a)) {
9591
return false;
9692
}
9793
}
@@ -100,6 +96,47 @@ class Solution {
10096
}
10197
```
10298

99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
bool areSentencesSimilar(vector<string>& sentence1, vector<string>& sentence2, vector<vector<string>>& similarPairs) {
105+
int m = sentence1.size(), n = sentence2.size();
106+
if (m != n) return false;
107+
unordered_set<string> s;
108+
for (auto e : similarPairs) s.insert(e[0] + "." + e[1]);
109+
for (int i = 0; i < n; ++i)
110+
{
111+
string a = sentence1[i], b = sentence2[i];
112+
if (a != b && !s.count(a + "." + b) && !s.count(b + "." + a)) return false;
113+
}
114+
return true;
115+
}
116+
};
117+
```
118+
119+
### **Go**
120+
121+
```go
122+
func areSentencesSimilar(sentence1 []string, sentence2 []string, similarPairs [][]string) bool {
123+
if len(sentence1) != len(sentence2) {
124+
return false
125+
}
126+
s := map[string]bool{}
127+
for _, e := range similarPairs {
128+
s[e[0]+"."+e[1]] = true
129+
}
130+
for i, a := range sentence1 {
131+
b := sentence2[i]
132+
if a != b && !s[a+"."+b] && !s[b+"."+a] {
133+
return false
134+
}
135+
}
136+
return true
137+
}
138+
```
139+
103140
### **...**
104141

105142
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
bool areSentencesSimilar(vector<string>& sentence1, vector<string>& sentence2, vector<vector<string>>& similarPairs) {
4+
int m = sentence1.size(), n = sentence2.size();
5+
if (m != n) return false;
6+
unordered_set<string> s;
7+
for (auto e : similarPairs) s.insert(e[0] + "." + e[1]);
8+
for (int i = 0; i < n; ++i)
9+
{
10+
string a = sentence1[i], b = sentence2[i];
11+
if (a != b && !s.count(a + "." + b) && !s.count(b + "." + a)) return false;
12+
}
13+
return true;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func areSentencesSimilar(sentence1 []string, sentence2 []string, similarPairs [][]string) bool {
2+
if len(sentence1) != len(sentence2) {
3+
return false
4+
}
5+
s := map[string]bool{}
6+
for _, e := range similarPairs {
7+
s[e[0]+"."+e[1]] = true
8+
}
9+
for i, a := range sentence1 {
10+
b := sentence2[i]
11+
if a != b && !s[a+"."+b] && !s[b+"."+a] {
12+
return false
13+
}
14+
}
15+
return true
16+
}

solution/0700-0799/0734.Sentence Similarity/Solution.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ public boolean areSentencesSimilar(String[] sentence1, String[] sentence2, List<
33
if (sentence1.length != sentence2.length) {
44
return false;
55
}
6-
Set<String> pairs = new HashSet<>();
7-
for (List<String> pair : similarPairs) {
8-
pairs.add(pair.get(0) + "." + pair.get(1));
6+
Set<String> s = new HashSet<>();
7+
for (List<String> e : similarPairs) {
8+
s.add(e.get(0) + "." + e.get(1));
99
}
1010
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) {
11+
String a = sentence1[i], b = sentence2[i];
12+
if (!a.equals(b) && !s.contains(a + "." + b) && !s.contains(b + "." + a)) {
1313
return false;
1414
}
1515
}

solution/0700-0799/0734.Sentence Similarity/Solution.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ def areSentencesSimilar(
44
) -> bool:
55
if len(sentence1) != len(sentence2):
66
return False
7-
pairs = {(word1, word2) for word1, word2 in similarPairs}
8-
for i in range(len(sentence1)):
9-
similar = (
10-
(sentence1[i], sentence2[i]) in pairs
11-
or (sentence2[i], sentence1[i]) in pairs
12-
or sentence1[i] == sentence2[i]
13-
)
14-
if not similar:
15-
return False
16-
return True
7+
s = {(a, b) for a, b in similarPairs}
8+
return all(
9+
a == b or (a, b) in s or (b, a) in s for a, b in zip(sentence1, sentence2)
10+
)

solution/0700-0799/0758.Bold Words in String/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def boldWords(self, words: List[str], s: str) -> str:
4949
break
5050
st, ed = t[j]
5151
if i < st:
52-
ans.append(s[i: st])
52+
ans.append(s[i:st])
5353
ans.append('<b>')
54-
ans.append(s[st: ed + 1])
54+
ans.append(s[st : ed + 1])
5555
ans.append('</b>')
5656
j += 1
5757
i = ed + 1

0 commit comments

Comments
 (0)