Skip to content

Commit b901bf8

Browse files
authored
feat: add solutions to lc problem: No.0734 (#4025)
No.0734.Sentence Similarity
1 parent 705fcd3 commit b901bf8

File tree

10 files changed

+355
-68
lines changed

10 files changed

+355
-68
lines changed

solution/0700-0799/0729.My Calendar I/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ myCalendar.book(20, 30); // return True, The event can be booked, as the first e
6363

6464
<!-- solution:start -->
6565

66-
### Solution 1
66+
### Solution 1: Ordered Set
67+
68+
We can use an ordered set to store the schedule. An ordered set can perform insert, delete, and search operations in $O(\log n)$ time. The elements in the ordered set are sorted by the $\textit{endTime}$ of the schedule in ascending order.
69+
70+
When calling the $\text{book}(start, end)$ method, we search for the first schedule in the ordered set with an end time greater than $\textit{start}$. If it exists and its start time is less than $\textit{end}$, it means there is a double booking, and we return $\text{false}$. Otherwise, we insert $\textit{end}$ as the key and $\textit{start}$ as the value into the ordered set and return $\text{true}$.
71+
72+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of schedules.
6773

6874
<!-- tabs:start -->
6975

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

+123-21
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ tags:
8080

8181
### 方法一:哈希表
8282

83+
我们首先判断 $\textit{sentence1}$ 和 $\textit{sentence2}$ 的长度是否相等,如果不相等则返回 $\text{false}$。
84+
85+
然后我们使用一个哈希表 $\textit{s}$ 来存储所有相似的单词对,对于 $\textit{similarPairs}$ 中的每一个单词对 $[x, y]$,我们将 $x$ 和 $y$ 加入到哈希表 $\textit{s}$ 中。
86+
87+
接下来我们遍历 $\textit{sentence1}$ 和 $\textit{sentence2}$,对于每一个位置 $i$,如果 $\textit{sentence1}[i]$ 不等于 $\textit{sentence2}[i]$,并且 $(\textit{sentence1}[i], \textit{sentence2}[i])$ 和 $(\textit{sentence2}[i], \textit{sentence1}[i])$ 都不在哈希表 $\textit{s}$ 中,那么返回 $\text{false}$。
88+
89+
如果遍历结束后都没有返回 $\text{false}$,说明 $\textit{sentence1}$ 和 $\textit{sentence2}$ 是相似的,返回 $\text{true}$。
90+
91+
时间复杂度 $O(L)$,空间复杂度 $O(L)$,其中 $L$ 为题目中所有字符串的长度之和。
92+
8393
<!-- tabs:start -->
8494

8595
#### Python3
@@ -91,10 +101,11 @@ class Solution:
91101
) -> bool:
92102
if len(sentence1) != len(sentence2):
93103
return False
94-
s = {(a, b) for a, b in similarPairs}
95-
return all(
96-
a == b or (a, b) in s or (b, a) in s for a, b in zip(sentence1, sentence2)
97-
)
104+
s = {(x, y) for x, y in similarPairs}
105+
for x, y in zip(sentence1, sentence2):
106+
if x != y and (x, y) not in s and (y, x) not in s:
107+
return False
108+
return True
98109
```
99110

100111
#### Java
@@ -106,13 +117,14 @@ class Solution {
106117
if (sentence1.length != sentence2.length) {
107118
return false;
108119
}
109-
Set<String> s = new HashSet<>();
110-
for (List<String> e : similarPairs) {
111-
s.add(e.get(0) + "." + e.get(1));
120+
Set<List<String>> s = new HashSet<>();
121+
for (var p : similarPairs) {
122+
s.add(p);
112123
}
113-
for (int i = 0; i < sentence1.length; ++i) {
114-
String a = sentence1[i], b = sentence2[i];
115-
if (!a.equals(b) && !s.contains(a + "." + b) && !s.contains(b + "." + a)) {
124+
for (int i = 0; i < sentence1.length; i++) {
125+
if (!sentence1[i].equals(sentence2[i])
126+
&& !s.contains(List.of(sentence1[i], sentence2[i]))
127+
&& !s.contains(List.of(sentence2[i], sentence1[i]))) {
116128
return false;
117129
}
118130
}
@@ -127,13 +139,18 @@ class Solution {
127139
class Solution {
128140
public:
129141
bool areSentencesSimilar(vector<string>& sentence1, vector<string>& sentence2, vector<vector<string>>& similarPairs) {
130-
int m = sentence1.size(), n = sentence2.size();
131-
if (m != n) return false;
142+
if (sentence1.size() != sentence2.size()) {
143+
return false;
144+
}
132145
unordered_set<string> s;
133-
for (auto e : similarPairs) s.insert(e[0] + "." + e[1]);
134-
for (int i = 0; i < n; ++i) {
135-
string a = sentence1[i], b = sentence2[i];
136-
if (a != b && !s.count(a + "." + b) && !s.count(b + "." + a)) return false;
146+
for (const auto& p : similarPairs) {
147+
s.insert(p[0] + "#" + p[1]);
148+
s.insert(p[1] + "#" + p[0]);
149+
}
150+
for (int i = 0; i < sentence1.size(); ++i) {
151+
if (sentence1[i] != sentence2[i] && !s.contains(sentence1[i] + "#" + sentence2[i])) {
152+
return false;
153+
}
137154
}
138155
return true;
139156
}
@@ -148,19 +165,104 @@ func areSentencesSimilar(sentence1 []string, sentence2 []string, similarPairs []
148165
return false
149166
}
150167
s := map[string]bool{}
151-
for _, e := range similarPairs {
152-
s[e[0]+"."+e[1]] = true
168+
for _, p := range similarPairs {
169+
s[p[0]+"#"+p[1]] = true
153170
}
154-
for i, a := range sentence1 {
155-
b := sentence2[i]
156-
if a != b && !s[a+"."+b] && !s[b+"."+a] {
171+
for i, x := range sentence1 {
172+
y := sentence2[i]
173+
if x != y && !s[x+"#"+y] && !s[y+"#"+x] {
157174
return false
158175
}
159176
}
160177
return true
161178
}
162179
```
163180

181+
#### TypeScript
182+
183+
```ts
184+
function areSentencesSimilar(
185+
sentence1: string[],
186+
sentence2: string[],
187+
similarPairs: string[][],
188+
): boolean {
189+
if (sentence1.length !== sentence2.length) {
190+
return false;
191+
}
192+
const s = new Set<string>();
193+
for (const [x, y] of similarPairs) {
194+
s.add(x + '#' + y);
195+
s.add(y + '#' + x);
196+
}
197+
for (let i = 0; i < sentence1.length; i++) {
198+
if (sentence1[i] !== sentence2[i] && !s.has(sentence1[i] + '#' + sentence2[i])) {
199+
return false;
200+
}
201+
}
202+
return true;
203+
}
204+
```
205+
206+
#### Rust
207+
208+
```rust
209+
use std::collections::HashSet;
210+
211+
impl Solution {
212+
pub fn are_sentences_similar(
213+
sentence1: Vec<String>,
214+
sentence2: Vec<String>,
215+
similar_pairs: Vec<Vec<String>>,
216+
) -> bool {
217+
if sentence1.len() != sentence2.len() {
218+
return false;
219+
}
220+
221+
let s: HashSet<(String, String)> = similar_pairs
222+
.into_iter()
223+
.map(|pair| (pair[0].clone(), pair[1].clone()))
224+
.collect();
225+
226+
for (x, y) in sentence1.iter().zip(sentence2.iter()) {
227+
if x != y
228+
&& !s.contains(&(x.clone(), y.clone()))
229+
&& !s.contains(&(y.clone(), x.clone()))
230+
{
231+
return false;
232+
}
233+
}
234+
true
235+
}
236+
}
237+
```
238+
239+
#### JavaScript
240+
241+
```js
242+
/**
243+
* @param {string[]} sentence1
244+
* @param {string[]} sentence2
245+
* @param {string[][]} similarPairs
246+
* @return {boolean}
247+
*/
248+
var areSentencesSimilar = function (sentence1, sentence2, similarPairs) {
249+
if (sentence1.length !== sentence2.length) {
250+
return false;
251+
}
252+
const s = new Set();
253+
for (const [x, y] of similarPairs) {
254+
s.add(x + '#' + y);
255+
s.add(y + '#' + x);
256+
}
257+
for (let i = 0; i < sentence1.length; i++) {
258+
if (sentence1[i] !== sentence2[i] && !s.has(sentence1[i] + '#' + sentence2[i])) {
259+
return false;
260+
}
261+
}
262+
return true;
263+
};
264+
```
265+
164266
<!-- tabs:end -->
165267

166268
<!-- solution:end -->

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

+124-22
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,17 @@ tags:
7878

7979
<!-- solution:start -->
8080

81-
### Solution 1
81+
### Solution 1: Hash Table
82+
83+
First, we check if the lengths of $\textit{sentence1}$ and $\textit{sentence2}$ are equal. If they are not equal, return $\text{false}$.
84+
85+
Then we use a hash table $\textit{s}$ to store all similar word pairs. For each word pair $[x, y]$ in $\textit{similarPairs}$, we add $x$ and $y$ to the hash table $\textit{s}$.
86+
87+
Next, we traverse $\textit{sentence1}$ and $\textit{sentence2}$. For each position $i$, if $\textit{sentence1}[i]$ is not equal to $\textit{sentence2}[i]$, and $(\textit{sentence1}[i], \textit{sentence2}[i])$ and $(\textit{sentence2}[i], \textit{sentence1}[i])$ are not in the hash table $\textit{s}$, then return $\text{false}$.
88+
89+
If the traversal ends without returning $\text{false}$, it means $\textit{sentence1}$ and $\textit{sentence2}$ are similar, so return $\text{true}$.
90+
91+
The time complexity is $O(L)$, and the space complexity is $O(L)$, where $L$ is the sum of the lengths of all strings in the problem.
8292

8393
<!-- tabs:start -->
8494

@@ -91,10 +101,11 @@ class Solution:
91101
) -> bool:
92102
if len(sentence1) != len(sentence2):
93103
return False
94-
s = {(a, b) for a, b in similarPairs}
95-
return all(
96-
a == b or (a, b) in s or (b, a) in s for a, b in zip(sentence1, sentence2)
97-
)
104+
s = {(x, y) for x, y in similarPairs}
105+
for x, y in zip(sentence1, sentence2):
106+
if x != y and (x, y) not in s and (y, x) not in s:
107+
return False
108+
return True
98109
```
99110

100111
#### Java
@@ -106,13 +117,14 @@ class Solution {
106117
if (sentence1.length != sentence2.length) {
107118
return false;
108119
}
109-
Set<String> s = new HashSet<>();
110-
for (List<String> e : similarPairs) {
111-
s.add(e.get(0) + "." + e.get(1));
120+
Set<List<String>> s = new HashSet<>();
121+
for (var p : similarPairs) {
122+
s.add(p);
112123
}
113-
for (int i = 0; i < sentence1.length; ++i) {
114-
String a = sentence1[i], b = sentence2[i];
115-
if (!a.equals(b) && !s.contains(a + "." + b) && !s.contains(b + "." + a)) {
124+
for (int i = 0; i < sentence1.length; i++) {
125+
if (!sentence1[i].equals(sentence2[i])
126+
&& !s.contains(List.of(sentence1[i], sentence2[i]))
127+
&& !s.contains(List.of(sentence2[i], sentence1[i]))) {
116128
return false;
117129
}
118130
}
@@ -127,13 +139,18 @@ class Solution {
127139
class Solution {
128140
public:
129141
bool areSentencesSimilar(vector<string>& sentence1, vector<string>& sentence2, vector<vector<string>>& similarPairs) {
130-
int m = sentence1.size(), n = sentence2.size();
131-
if (m != n) return false;
142+
if (sentence1.size() != sentence2.size()) {
143+
return false;
144+
}
132145
unordered_set<string> s;
133-
for (auto e : similarPairs) s.insert(e[0] + "." + e[1]);
134-
for (int i = 0; i < n; ++i) {
135-
string a = sentence1[i], b = sentence2[i];
136-
if (a != b && !s.count(a + "." + b) && !s.count(b + "." + a)) return false;
146+
for (const auto& p : similarPairs) {
147+
s.insert(p[0] + "#" + p[1]);
148+
s.insert(p[1] + "#" + p[0]);
149+
}
150+
for (int i = 0; i < sentence1.size(); ++i) {
151+
if (sentence1[i] != sentence2[i] && !s.contains(sentence1[i] + "#" + sentence2[i])) {
152+
return false;
153+
}
137154
}
138155
return true;
139156
}
@@ -148,19 +165,104 @@ func areSentencesSimilar(sentence1 []string, sentence2 []string, similarPairs []
148165
return false
149166
}
150167
s := map[string]bool{}
151-
for _, e := range similarPairs {
152-
s[e[0]+"."+e[1]] = true
168+
for _, p := range similarPairs {
169+
s[p[0]+"#"+p[1]] = true
153170
}
154-
for i, a := range sentence1 {
155-
b := sentence2[i]
156-
if a != b && !s[a+"."+b] && !s[b+"."+a] {
171+
for i, x := range sentence1 {
172+
y := sentence2[i]
173+
if x != y && !s[x+"#"+y] && !s[y+"#"+x] {
157174
return false
158175
}
159176
}
160177
return true
161178
}
162179
```
163180

181+
#### TypeScript
182+
183+
```ts
184+
function areSentencesSimilar(
185+
sentence1: string[],
186+
sentence2: string[],
187+
similarPairs: string[][],
188+
): boolean {
189+
if (sentence1.length !== sentence2.length) {
190+
return false;
191+
}
192+
const s = new Set<string>();
193+
for (const [x, y] of similarPairs) {
194+
s.add(x + '#' + y);
195+
s.add(y + '#' + x);
196+
}
197+
for (let i = 0; i < sentence1.length; i++) {
198+
if (sentence1[i] !== sentence2[i] && !s.has(sentence1[i] + '#' + sentence2[i])) {
199+
return false;
200+
}
201+
}
202+
return true;
203+
}
204+
```
205+
206+
#### Rust
207+
208+
```rust
209+
use std::collections::HashSet;
210+
211+
impl Solution {
212+
pub fn are_sentences_similar(
213+
sentence1: Vec<String>,
214+
sentence2: Vec<String>,
215+
similar_pairs: Vec<Vec<String>>,
216+
) -> bool {
217+
if sentence1.len() != sentence2.len() {
218+
return false;
219+
}
220+
221+
let s: HashSet<(String, String)> = similar_pairs
222+
.into_iter()
223+
.map(|pair| (pair[0].clone(), pair[1].clone()))
224+
.collect();
225+
226+
for (x, y) in sentence1.iter().zip(sentence2.iter()) {
227+
if x != y
228+
&& !s.contains(&(x.clone(), y.clone()))
229+
&& !s.contains(&(y.clone(), x.clone()))
230+
{
231+
return false;
232+
}
233+
}
234+
true
235+
}
236+
}
237+
```
238+
239+
#### JavaScript
240+
241+
```js
242+
/**
243+
* @param {string[]} sentence1
244+
* @param {string[]} sentence2
245+
* @param {string[][]} similarPairs
246+
* @return {boolean}
247+
*/
248+
var areSentencesSimilar = function (sentence1, sentence2, similarPairs) {
249+
if (sentence1.length !== sentence2.length) {
250+
return false;
251+
}
252+
const s = new Set();
253+
for (const [x, y] of similarPairs) {
254+
s.add(x + '#' + y);
255+
s.add(y + '#' + x);
256+
}
257+
for (let i = 0; i < sentence1.length; i++) {
258+
if (sentence1[i] !== sentence2[i] && !s.has(sentence1[i] + '#' + sentence2[i])) {
259+
return false;
260+
}
261+
}
262+
return true;
263+
};
264+
```
265+
164266
<!-- tabs:end -->
165267

166268
<!-- solution:end -->

0 commit comments

Comments
 (0)