Skip to content

Commit faeca51

Browse files
committed
feat: add solutions to lc problem: No.1813
No.1813.Sentence Similarity III
1 parent 17b36bb commit faeca51

File tree

6 files changed

+167
-252
lines changed

6 files changed

+167
-252
lines changed

solution/1800-1899/1813.Sentence Similarity III/README.md

Lines changed: 65 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@
5555

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

58-
把句子分割成单词数组,然后通过公共前后缀进行判断
58+
**方法一:双指针**
59+
60+
我们将两个句子分别用空格分割成单词数组 `words1``words2`,假设 `words1``words2` 的长度分别为 $m$ 和 $n$,不妨设 $m \geq n$。
61+
62+
我们使用双指针 $i$ 和 $j$,初始时 $i = j = 0$。接下来,我们循环判断 `words1[i]` 是否等于 `words2[i]`,是则指针 $i$ 继续右移;然后我们循环判断 `words1[m - 1 - j]` 是否等于 `words2[n - 1 - j]`,是则指针 $j$ 继续右移。
63+
64+
循环结束后,如果 $i + j \geq n$,说明两个句子相似,返回 `true`,否则返回 `false`
65+
66+
时间复杂度 $O(L)$,空间复杂度 $O(L)$。其中 $L$ 为两个句子的长度之和。
5967

6068
<!-- tabs:start -->
6169

@@ -66,23 +74,17 @@
6674
```python
6775
class Solution:
6876
def areSentencesSimilar(self, sentence1: str, sentence2: str) -> bool:
69-
if sentence1 == sentence2:
70-
return True
71-
n1, n2 = len(sentence1), len(sentence2)
72-
if n1 == n2:
73-
return False
74-
if n1 < n2:
75-
sentence1, sentence2 = sentence2, sentence1
7677
words1, words2 = sentence1.split(), sentence2.split()
78+
m, n = len(words1), len(words2)
79+
if m < n:
80+
words1, words2 = words2, words1
81+
m, n = n, m
7782
i = j = 0
78-
n1, n2 = len(words1), len(words2)
79-
while i < n2 and words1[i] == words2[i]:
83+
while i < n and words1[i] == words2[i]:
8084
i += 1
81-
if i == n2:
82-
return True
83-
while j < n2 and words1[n1 - 1 - j] == words2[n2 - 1 - j]:
85+
while j < n and words1[m - 1 - j] == words2[n - 1 - j]:
8486
j += 1
85-
return j == n2 or i + j == n2
87+
return i + j >= n
8688
```
8789

8890
### **Java**
@@ -92,105 +94,80 @@ class Solution:
9294
```java
9395
class Solution {
9496
public boolean areSentencesSimilar(String sentence1, String sentence2) {
95-
if (sentence1.equals(sentence2)) {
96-
return true;
97-
}
98-
int n1 = sentence1.length(), n2 = sentence2.length();
99-
if (n1 == n2) {
100-
return false;
101-
}
102-
if (n1 < n2) {
103-
String t = sentence1;
104-
sentence1 = sentence2;
105-
sentence2 = t;
106-
}
10797
String[] words1 = sentence1.split(" ");
10898
String[] words2 = sentence2.split(" ");
99+
if (words1.length < words2.length) {
100+
String[] t = words1;
101+
words1 = words2;
102+
words2 = t;
103+
}
104+
int m = words1.length, n = words2.length;
109105
int i = 0, j = 0;
110-
n1 = words1.length;
111-
n2 = words2.length;
112-
while (i < n2 && words1[i].equals(words2[i])) {
106+
while (i < n && words1[i].equals(words2[i])) {
113107
++i;
114108
}
115-
if (i == n2) {
116-
return true;
117-
}
118-
while (j < n2 && words1[n1 - 1 - j].equals(words2[n2 - 1 - j])) {
109+
while (j < n && words1[m - 1 - j].equals(words2[n - 1 - j])) {
119110
++j;
120111
}
121-
return j == n2 || i + j == n2;
112+
return i == n || j == n || i + j >= n;
122113
}
123114
}
124115
```
125116

126-
### **Go**
127-
128-
```go
129-
func areSentencesSimilar(sentence1 string, sentence2 string) bool {
130-
if sentence1 == sentence2 {
131-
return true
132-
}
133-
l1, l2 := len(sentence1), len(sentence2)
134-
if l1 == l2 {
135-
return false
136-
}
137-
if l1 < l2 {
138-
sentence1, sentence2 = sentence2, sentence1
139-
}
140-
i, j := 0, 0
141-
w1, w2 := strings.Fields(sentence1), strings.Fields(sentence2)
142-
l1, l2 = len(w1), len(w2)
143-
for i < l2 && w1[i] == w2[i] {
144-
i++
145-
}
146-
if i == l2 {
147-
return true
148-
}
149-
for j < l2 && w1[l1-1-j] == w2[l2-1-j] {
150-
j++
151-
}
152-
return j == l2 || i+j == l2
153-
}
154-
```
155-
156117
### **C++**
157118

158119
```cpp
159120
class Solution {
160121
public:
161122
bool areSentencesSimilar(string sentence1, string sentence2) {
162-
if (sentence1 == sentence2) return true;
163-
int n1 = sentence1.size(), n2 = sentence2.size();
164-
if (n1 == n2) return false;
165-
166-
if (n1 < n2) swap(sentence1, sentence2);
167-
auto words1 = split(sentence1);
168-
auto words2 = split(sentence2);
123+
vector<string> words1 = split(sentence1, ' ');
124+
vector<string> words2 = split(sentence2, ' ');
125+
if (words1.size() < words2.size()) {
126+
swap(words1, words2);
127+
}
128+
int m = words1.size(), n = words2.size();
169129
int i = 0, j = 0;
170-
n1 = words1.size(), n2 = words2.size();
171-
172-
while (i < n2 && words1[i] == words2[i]) ++i;
173-
if (i == n2) return true;
174-
175-
while (j < n2 && words1[n1 - 1 - j] == words2[n2 - 1 - j]) ++j;
176-
return j == n2 || i + j == n2;
130+
while (i < n && words1[i] == words2[i]) {
131+
++i;
132+
}
133+
while (j < n && words1[m - 1 - j] == words2[n - 1 - j]) {
134+
++j;
135+
}
136+
return i + j >= n;
177137
}
178138

179-
vector<string> split(const string& str) {
180-
vector<string> words;
181-
int i = str.find_first_not_of(' ');
182-
int j = str.find_first_of(' ', i);
183-
while (j != string::npos) {
184-
words.emplace_back(str.substr(i, j - i));
185-
i = str.find_first_not_of(' ', j);
186-
j = str.find_first_of(' ', i);
139+
vector<string> split(string& s, char delim) {
140+
stringstream ss(s);
141+
string item;
142+
vector<string> res;
143+
while (getline(ss, item, delim)) {
144+
res.emplace_back(item);
187145
}
188-
words.emplace_back(str.substr(i));
189-
return words;
146+
return res;
190147
}
191148
};
192149
```
193150

151+
### **Go**
152+
153+
```go
154+
func areSentencesSimilar(sentence1 string, sentence2 string) bool {
155+
words1, words2 := strings.Fields(sentence1), strings.Fields(sentence2)
156+
if len(words1) < len(words2) {
157+
words1, words2 = words2, words1
158+
}
159+
m, n := len(words1), len(words2)
160+
i, j := 0, 0
161+
for i < n && words1[i] == words2[i] {
162+
i++
163+
}
164+
for j < n && words1[m-1-j] == words2[n-1-j] {
165+
j++
166+
}
167+
return i+j >= n
168+
}
169+
```
170+
194171
### **...**
195172

196173
```

solution/1800-1899/1813.Sentence Similarity III/README_EN.md

Lines changed: 56 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -53,129 +53,98 @@
5353
```python
5454
class Solution:
5555
def areSentencesSimilar(self, sentence1: str, sentence2: str) -> bool:
56-
if sentence1 == sentence2:
57-
return True
58-
n1, n2 = len(sentence1), len(sentence2)
59-
if n1 == n2:
60-
return False
61-
if n1 < n2:
62-
sentence1, sentence2 = sentence2, sentence1
6356
words1, words2 = sentence1.split(), sentence2.split()
57+
m, n = len(words1), len(words2)
58+
if m < n:
59+
words1, words2 = words2, words1
60+
m, n = n, m
6461
i = j = 0
65-
n1, n2 = len(words1), len(words2)
66-
while i < n2 and words1[i] == words2[i]:
62+
while i < n and words1[i] == words2[i]:
6763
i += 1
68-
if i == n2:
69-
return True
70-
while j < n2 and words1[n1 - 1 - j] == words2[n2 - 1 - j]:
64+
while j < n and words1[m - 1 - j] == words2[n - 1 - j]:
7165
j += 1
72-
return j == n2 or i + j == n2
66+
return i + j >= n
7367
```
7468

7569
### **Java**
7670

7771
```java
7872
class Solution {
7973
public boolean areSentencesSimilar(String sentence1, String sentence2) {
80-
if (sentence1.equals(sentence2)) {
81-
return true;
82-
}
83-
int n1 = sentence1.length(), n2 = sentence2.length();
84-
if (n1 == n2) {
85-
return false;
86-
}
87-
if (n1 < n2) {
88-
String t = sentence1;
89-
sentence1 = sentence2;
90-
sentence2 = t;
91-
}
9274
String[] words1 = sentence1.split(" ");
9375
String[] words2 = sentence2.split(" ");
76+
if (words1.length < words2.length) {
77+
String[] t = words1;
78+
words1 = words2;
79+
words2 = t;
80+
}
81+
int m = words1.length, n = words2.length;
9482
int i = 0, j = 0;
95-
n1 = words1.length;
96-
n2 = words2.length;
97-
while (i < n2 && words1[i].equals(words2[i])) {
83+
while (i < n && words1[i].equals(words2[i])) {
9884
++i;
9985
}
100-
if (i == n2) {
101-
return true;
102-
}
103-
while (j < n2 && words1[n1 - 1 - j].equals(words2[n2 - 1 - j])) {
86+
while (j < n && words1[m - 1 - j].equals(words2[n - 1 - j])) {
10487
++j;
10588
}
106-
return j == n2 || i + j == n2;
89+
return i == n || j == n || i + j >= n;
10790
}
10891
}
10992
```
11093

111-
### **Go**
112-
113-
```go
114-
func areSentencesSimilar(sentence1 string, sentence2 string) bool {
115-
if sentence1 == sentence2 {
116-
return true
117-
}
118-
l1, l2 := len(sentence1), len(sentence2)
119-
if l1 == l2 {
120-
return false
121-
}
122-
if l1 < l2 {
123-
sentence1, sentence2 = sentence2, sentence1
124-
}
125-
i, j := 0, 0
126-
w1, w2 := strings.Fields(sentence1), strings.Fields(sentence2)
127-
l1, l2 = len(w1), len(w2)
128-
for i < l2 && w1[i] == w2[i] {
129-
i++
130-
}
131-
if i == l2 {
132-
return true
133-
}
134-
for j < l2 && w1[l1-1-j] == w2[l2-1-j] {
135-
j++
136-
}
137-
return j == l2 || i+j == l2
138-
}
139-
```
140-
14194
### **C++**
14295

14396
```cpp
14497
class Solution {
14598
public:
14699
bool areSentencesSimilar(string sentence1, string sentence2) {
147-
if (sentence1 == sentence2) return true;
148-
int n1 = sentence1.size(), n2 = sentence2.size();
149-
if (n1 == n2) return false;
150-
151-
if (n1 < n2) swap(sentence1, sentence2);
152-
auto words1 = split(sentence1);
153-
auto words2 = split(sentence2);
100+
vector<string> words1 = split(sentence1, ' ');
101+
vector<string> words2 = split(sentence2, ' ');
102+
if (words1.size() < words2.size()) {
103+
swap(words1, words2);
104+
}
105+
int m = words1.size(), n = words2.size();
154106
int i = 0, j = 0;
155-
n1 = words1.size(), n2 = words2.size();
156-
157-
while (i < n2 && words1[i] == words2[i]) ++i;
158-
if (i == n2) return true;
159-
160-
while (j < n2 && words1[n1 - 1 - j] == words2[n2 - 1 - j]) ++j;
161-
return j == n2 || i + j == n2;
107+
while (i < n && words1[i] == words2[i]) {
108+
++i;
109+
}
110+
while (j < n && words1[m - 1 - j] == words2[n - 1 - j]) {
111+
++j;
112+
}
113+
return i + j >= n;
162114
}
163115

164-
vector<string> split(const string& str) {
165-
vector<string> words;
166-
int i = str.find_first_not_of(' ');
167-
int j = str.find_first_of(' ', i);
168-
while (j != string::npos) {
169-
words.emplace_back(str.substr(i, j - i));
170-
i = str.find_first_not_of(' ', j);
171-
j = str.find_first_of(' ', i);
116+
vector<string> split(string& s, char delim) {
117+
stringstream ss(s);
118+
string item;
119+
vector<string> res;
120+
while (getline(ss, item, delim)) {
121+
res.emplace_back(item);
172122
}
173-
words.emplace_back(str.substr(i));
174-
return words;
123+
return res;
175124
}
176125
};
177126
```
178127

128+
### **Go**
129+
130+
```go
131+
func areSentencesSimilar(sentence1 string, sentence2 string) bool {
132+
words1, words2 := strings.Fields(sentence1), strings.Fields(sentence2)
133+
if len(words1) < len(words2) {
134+
words1, words2 = words2, words1
135+
}
136+
m, n := len(words1), len(words2)
137+
i, j := 0, 0
138+
for i < n && words1[i] == words2[i] {
139+
i++
140+
}
141+
for j < n && words1[m-1-j] == words2[n-1-j] {
142+
j++
143+
}
144+
return i+j >= n
145+
}
146+
```
147+
179148
### **...**
180149

181150
```

0 commit comments

Comments
 (0)