Skip to content

Commit

Permalink
feat: update solutions to lc problem: No.0734
Browse files Browse the repository at this point in the history
No.0734.Sentence Similarity
  • Loading branch information
yanglbme committed Jul 13, 2022
1 parent 81bb836 commit 45ce2d1
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 43 deletions.
2 changes: 1 addition & 1 deletion solution/0600-0699/0605.Can Place Flowers/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
flowerbed = [0] + flowerbed + [0]
for i in range(1, len(flowerbed) - 1):
if sum(flowerbed[i - 1: i + 2]) == 0:
if sum(flowerbed[i - 1 : i + 2]) == 0:
flowerbed[i] = 1
n -= 1
return n <= 0
4 changes: 2 additions & 2 deletions solution/0600-0699/0616.Add Bold Tag in String/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def addBoldTag(self, s: str, words: List[str]) -> str:
break
st, ed = t[j]
if i < st:
ans.append(s[i: st])
ans.append(s[i:st])
ans.append('<b>')
ans.append(s[st: ed + 1])
ans.append(s[st : ed + 1])
ans.append('</b>')
j += 1
i = ed + 1
Expand Down
61 changes: 49 additions & 12 deletions solution/0700-0799/0734.Sentence Similarity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

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

哈希表”实现。
**方法一:哈希表**

<!-- tabs:start -->

Expand All @@ -77,12 +77,8 @@ class Solution:
def areSentencesSimilar(self, sentence1: List[str], sentence2: List[str], similarPairs: List[List[str]]) -> bool:
if len(sentence1) != len(sentence2):
return False
pairs = {(word1, word2) for word1, word2 in similarPairs}
for i in range(len(sentence1)):
similar = (sentence1[i], sentence2[i]) in pairs or (sentence2[i], sentence1[i]) in pairs or sentence1[i] == sentence2[i]
if not similar:
return False
return True
s = {(a, b) for a, b in similarPairs}
return all(a == b or (a, b) in s or (b, a) in s for a, b in zip(sentence1, sentence2))
```

### **Java**
Expand All @@ -95,13 +91,13 @@ class Solution {
if (sentence1.length != sentence2.length) {
return false;
}
Set<String> pairs = new HashSet<>();
for (List<String> pair : similarPairs) {
pairs.add(pair.get(0) + "." + pair.get(1));
Set<String> s = new HashSet<>();
for (List<String> e : similarPairs) {
s.add(e.get(0) + "." + e.get(1));
}
for (int i = 0; i < sentence1.length; ++i) {
boolean similar = pairs.contains(sentence1[i] + "." + sentence2[i]) || pairs.contains(sentence2[i] + "." + sentence1[i]) || sentence1[i].equals(sentence2[i]);
if (!similar) {
String a = sentence1[i], b = sentence2[i];
if (!a.equals(b) && !s.contains(a + "." + b) && !s.contains(b + "." + a)) {
return false;
}
}
Expand All @@ -110,6 +106,47 @@ class Solution {
}
```

### **C++**

```cpp
class Solution {
public:
bool areSentencesSimilar(vector<string>& sentence1, vector<string>& sentence2, vector<vector<string>>& similarPairs) {
int m = sentence1.size(), n = sentence2.size();
if (m != n) return false;
unordered_set<string> s;
for (auto e : similarPairs) s.insert(e[0] + "." + e[1]);
for (int i = 0; i < n; ++i)
{
string a = sentence1[i], b = sentence2[i];
if (a != b && !s.count(a + "." + b) && !s.count(b + "." + a)) return false;
}
return true;
}
};
```
### **Go**
```go
func areSentencesSimilar(sentence1 []string, sentence2 []string, similarPairs [][]string) bool {
if len(sentence1) != len(sentence2) {
return false
}
s := map[string]bool{}
for _, e := range similarPairs {
s[e[0]+"."+e[1]] = true
}
for i, a := range sentence1 {
b := sentence2[i]
if a != b && !s[a+"."+b] && !s[b+"."+a] {
return false
}
}
return true
}
```

### **...**

```
Expand Down
59 changes: 48 additions & 11 deletions solution/0700-0799/0734.Sentence Similarity/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,8 @@ class Solution:
def areSentencesSimilar(self, sentence1: List[str], sentence2: List[str], similarPairs: List[List[str]]) -> bool:
if len(sentence1) != len(sentence2):
return False
pairs = {(word1, word2) for word1, word2 in similarPairs}
for i in range(len(sentence1)):
similar = (sentence1[i], sentence2[i]) in pairs or (sentence2[i], sentence1[i]) in pairs or sentence1[i] == sentence2[i]
if not similar:
return False
return True
s = {(a, b) for a, b in similarPairs}
return all(a == b or (a, b) in s or (b, a) in s for a, b in zip(sentence1, sentence2))
```

### **Java**
Expand All @@ -85,13 +81,13 @@ class Solution {
if (sentence1.length != sentence2.length) {
return false;
}
Set<String> pairs = new HashSet<>();
for (List<String> pair : similarPairs) {
pairs.add(pair.get(0) + "." + pair.get(1));
Set<String> s = new HashSet<>();
for (List<String> e : similarPairs) {
s.add(e.get(0) + "." + e.get(1));
}
for (int i = 0; i < sentence1.length; ++i) {
boolean similar = pairs.contains(sentence1[i] + "." + sentence2[i]) || pairs.contains(sentence2[i] + "." + sentence1[i]) || sentence1[i].equals(sentence2[i]);
if (!similar) {
String a = sentence1[i], b = sentence2[i];
if (!a.equals(b) && !s.contains(a + "." + b) && !s.contains(b + "." + a)) {
return false;
}
}
Expand All @@ -100,6 +96,47 @@ class Solution {
}
```

### **C++**

```cpp
class Solution {
public:
bool areSentencesSimilar(vector<string>& sentence1, vector<string>& sentence2, vector<vector<string>>& similarPairs) {
int m = sentence1.size(), n = sentence2.size();
if (m != n) return false;
unordered_set<string> s;
for (auto e : similarPairs) s.insert(e[0] + "." + e[1]);
for (int i = 0; i < n; ++i)
{
string a = sentence1[i], b = sentence2[i];
if (a != b && !s.count(a + "." + b) && !s.count(b + "." + a)) return false;
}
return true;
}
};
```
### **Go**
```go
func areSentencesSimilar(sentence1 []string, sentence2 []string, similarPairs [][]string) bool {
if len(sentence1) != len(sentence2) {
return false
}
s := map[string]bool{}
for _, e := range similarPairs {
s[e[0]+"."+e[1]] = true
}
for i, a := range sentence1 {
b := sentence2[i]
if a != b && !s[a+"."+b] && !s[b+"."+a] {
return false
}
}
return true
}
```

### **...**

```
Expand Down
15 changes: 15 additions & 0 deletions solution/0700-0799/0734.Sentence Similarity/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
bool areSentencesSimilar(vector<string>& sentence1, vector<string>& sentence2, vector<vector<string>>& similarPairs) {
int m = sentence1.size(), n = sentence2.size();
if (m != n) return false;
unordered_set<string> s;
for (auto e : similarPairs) s.insert(e[0] + "." + e[1]);
for (int i = 0; i < n; ++i)
{
string a = sentence1[i], b = sentence2[i];
if (a != b && !s.count(a + "." + b) && !s.count(b + "." + a)) return false;
}
return true;
}
};
16 changes: 16 additions & 0 deletions solution/0700-0799/0734.Sentence Similarity/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
func areSentencesSimilar(sentence1 []string, sentence2 []string, similarPairs [][]string) bool {
if len(sentence1) != len(sentence2) {
return false
}
s := map[string]bool{}
for _, e := range similarPairs {
s[e[0]+"."+e[1]] = true
}
for i, a := range sentence1 {
b := sentence2[i]
if a != b && !s[a+"."+b] && !s[b+"."+a] {
return false
}
}
return true
}
10 changes: 5 additions & 5 deletions solution/0700-0799/0734.Sentence Similarity/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ public boolean areSentencesSimilar(String[] sentence1, String[] sentence2, List<
if (sentence1.length != sentence2.length) {
return false;
}
Set<String> pairs = new HashSet<>();
for (List<String> pair : similarPairs) {
pairs.add(pair.get(0) + "." + pair.get(1));
Set<String> s = new HashSet<>();
for (List<String> e : similarPairs) {
s.add(e.get(0) + "." + e.get(1));
}
for (int i = 0; i < sentence1.length; ++i) {
boolean similar = pairs.contains(sentence1[i] + "." + sentence2[i]) || pairs.contains(sentence2[i] + "." + sentence1[i]) || sentence1[i].equals(sentence2[i]);
if (!similar) {
String a = sentence1[i], b = sentence2[i];
if (!a.equals(b) && !s.contains(a + "." + b) && !s.contains(b + "." + a)) {
return false;
}
}
Expand Down
14 changes: 4 additions & 10 deletions solution/0700-0799/0734.Sentence Similarity/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ def areSentencesSimilar(
) -> bool:
if len(sentence1) != len(sentence2):
return False
pairs = {(word1, word2) for word1, word2 in similarPairs}
for i in range(len(sentence1)):
similar = (
(sentence1[i], sentence2[i]) in pairs
or (sentence2[i], sentence1[i]) in pairs
or sentence1[i] == sentence2[i]
)
if not similar:
return False
return True
s = {(a, b) for a, b in similarPairs}
return all(
a == b or (a, b) in s or (b, a) in s for a, b in zip(sentence1, sentence2)
)
4 changes: 2 additions & 2 deletions solution/0700-0799/0758.Bold Words in String/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def boldWords(self, words: List[str], s: str) -> str:
break
st, ed = t[j]
if i < st:
ans.append(s[i: st])
ans.append(s[i:st])
ans.append('<b>')
ans.append(s[st: ed + 1])
ans.append(s[st : ed + 1])
ans.append('</b>')
j += 1
i = ed + 1
Expand Down

0 comments on commit 45ce2d1

Please sign in to comment.