Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e8faeee

Browse files
feat: add solutions to lc problem: No.1813
No.1813.Sentence Similarity III
1 parent 7ade880 commit e8faeee

File tree

6 files changed

+233
-43
lines changed

6 files changed

+233
-43
lines changed

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

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555

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

58+
把句子分割成单词数组,然后通过公共前后缀进行判断
59+
5860
<!-- tabs:start -->
5961

6062
### **Python3**
@@ -73,15 +75,14 @@ class Solution:
7375
sentence1, sentence2 = sentence2, sentence1
7476
words1, words2 = sentence1.split(), sentence2.split()
7577
i = j = 0
76-
while i < len(words2) and words1[i] == words2[i]:
78+
n1, n2 = len(words1), len(words2)
79+
while i < n2 and words1[i] == words2[i]:
7780
i += 1
78-
if i == len(words2):
81+
if i == n2:
7982
return True
80-
while j < len(words2) and words1[len(words1) - 1 - j] == words2[len(words2) - 1 - j]:
83+
while j < n2 and words1[n1 - 1 - j] == words2[n2 - 1 - j]:
8184
j += 1
82-
if j == len(words2):
83-
return True
84-
return i + j == len(words2)
85+
return j == n2 or i + j == n2
8586
```
8687

8788
### **Java**
@@ -91,7 +92,7 @@ class Solution:
9192
```java
9293
class Solution {
9394
public boolean areSentencesSimilar(String sentence1, String sentence2) {
94-
if (Objects.equals(sentence1, sentence2)) {
95+
if (sentence1.equals(sentence2)) {
9596
return true;
9697
}
9798
int n1 = sentence1.length(), n2 = sentence2.length();
@@ -106,23 +107,90 @@ class Solution {
106107
String[] words1 = sentence1.split(" ");
107108
String[] words2 = sentence2.split(" ");
108109
int i = 0, j = 0;
109-
while (i < words2.length && Objects.equals(words1[i], words2[i])) {
110+
n1 = words1.length;
111+
n2 = words2.length;
112+
while (i < n2 && words1[i].equals(words2[i])) {
110113
++i;
111114
}
112-
if (i == words2.length) {
115+
if (i == n2) {
113116
return true;
114117
}
115-
while (j < words2.length && Objects.equals(words1[words1.length - 1 - j], words2[words2.length - 1 - j])) {
118+
while (j < n2 && words1[n1 - 1 - j].equals(words2[n2 - 1 - j])) {
116119
++j;
117120
}
118-
if (j == words2.length) {
119-
return true;
120-
}
121-
return i + j == words2.length;
121+
return j == n2 || i + j == n2;
122122
}
123123
}
124124
```
125125

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+
156+
### **C++**
157+
158+
```cpp
159+
class Solution {
160+
public:
161+
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);
169+
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;
177+
}
178+
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);
187+
}
188+
words.emplace_back(str.substr(i));
189+
return words;
190+
}
191+
};
192+
```
193+
126194
### **...**
127195
128196
```

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

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,22 @@ class Solution:
6969
sentence1, sentence2 = sentence2, sentence1
7070
words1, words2 = sentence1.split(), sentence2.split()
7171
i = j = 0
72-
while i < len(words2) and words1[i] == words2[i]:
72+
n1, n2 = len(words1), len(words2)
73+
while i < n2 and words1[i] == words2[i]:
7374
i += 1
74-
if i == len(words2):
75+
if i == n2:
7576
return True
76-
while j < len(words2) and words1[len(words1) - 1 - j] == words2[len(words2) - 1 - j]:
77+
while j < n2 and words1[n1 - 1 - j] == words2[n2 - 1 - j]:
7778
j += 1
78-
if j == len(words2):
79-
return True
80-
return i + j == len(words2)
79+
return j == n2 or i + j == n2
8180
```
8281

8382
### **Java**
8483

8584
```java
8685
class Solution {
8786
public boolean areSentencesSimilar(String sentence1, String sentence2) {
88-
if (Objects.equals(sentence1, sentence2)) {
87+
if (sentence1.equals(sentence2)) {
8988
return true;
9089
}
9190
int n1 = sentence1.length(), n2 = sentence2.length();
@@ -100,23 +99,90 @@ class Solution {
10099
String[] words1 = sentence1.split(" ");
101100
String[] words2 = sentence2.split(" ");
102101
int i = 0, j = 0;
103-
while (i < words2.length && Objects.equals(words1[i], words2[i])) {
102+
n1 = words1.length;
103+
n2 = words2.length;
104+
while (i < n2 && words1[i].equals(words2[i])) {
104105
++i;
105106
}
106-
if (i == words2.length) {
107+
if (i == n2) {
107108
return true;
108109
}
109-
while (j < words2.length && Objects.equals(words1[words1.length - 1 - j], words2[words2.length - 1 - j])) {
110+
while (j < n2 && words1[n1 - 1 - j].equals(words2[n2 - 1 - j])) {
110111
++j;
111112
}
112-
if (j == words2.length) {
113-
return true;
114-
}
115-
return i + j == words2.length;
113+
return j == n2 || i + j == n2;
116114
}
117115
}
118116
```
119117

118+
### **Go**
119+
120+
```go
121+
func areSentencesSimilar(sentence1 string, sentence2 string) bool {
122+
if sentence1 == sentence2 {
123+
return true
124+
}
125+
l1, l2 := len(sentence1), len(sentence2)
126+
if l1 == l2 {
127+
return false
128+
}
129+
if l1 < l2 {
130+
sentence1, sentence2 = sentence2, sentence1
131+
}
132+
i, j := 0, 0
133+
w1, w2 := strings.Fields(sentence1), strings.Fields(sentence2)
134+
l1, l2 = len(w1), len(w2)
135+
for i < l2 && w1[i] == w2[i] {
136+
i++
137+
}
138+
if i == l2 {
139+
return true
140+
}
141+
for j < l2 && w1[l1-1-j] == w2[l2-1-j] {
142+
j++
143+
}
144+
return j == l2 || i+j == l2
145+
}
146+
```
147+
148+
### **C++**
149+
150+
```cpp
151+
class Solution {
152+
public:
153+
bool areSentencesSimilar(string sentence1, string sentence2) {
154+
if (sentence1 == sentence2) return true;
155+
int n1 = sentence1.size(), n2 = sentence2.size();
156+
if (n1 == n2) return false;
157+
158+
if (n1 < n2) swap(sentence1, sentence2);
159+
auto words1 = split(sentence1);
160+
auto words2 = split(sentence2);
161+
int i = 0, j = 0;
162+
n1 = words1.size(), n2 = words2.size();
163+
164+
while (i < n2 && words1[i] == words2[i]) ++i;
165+
if (i == n2) return true;
166+
167+
while (j < n2 && words1[n1 - 1 - j] == words2[n2 - 1 - j]) ++j;
168+
return j == n2 || i + j == n2;
169+
}
170+
171+
vector<string> split(const string &str) {
172+
vector<string> words;
173+
int i = str.find_first_not_of(' ');
174+
int j = str.find_first_of(' ', i);
175+
while (j != string::npos) {
176+
words.emplace_back(str.substr(i, j - i));
177+
i = str.find_first_not_of(' ', j);
178+
j = str.find_first_of(' ', i);
179+
}
180+
words.emplace_back(str.substr(i));
181+
return words;
182+
}
183+
};
184+
```
185+
120186
### **...**
121187
122188
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
bool areSentencesSimilar(string sentence1, string sentence2) {
4+
if (sentence1 == sentence2) return true;
5+
int n1 = sentence1.size(), n2 = sentence2.size();
6+
if (n1 == n2) return false;
7+
8+
if (n1 < n2) swap(sentence1, sentence2);
9+
auto words1 = split(sentence1);
10+
auto words2 = split(sentence2);
11+
int i = 0, j = 0;
12+
n1 = words1.size(), n2 = words2.size();
13+
14+
while (i < n2 && words1[i] == words2[i]) ++i;
15+
if (i == n2) return true;
16+
17+
while (j < n2 && words1[n1 - 1 - j] == words2[n2 - 1 - j]) ++j;
18+
return j == n2 || i + j == n2;
19+
}
20+
21+
vector<string> split(const string &str) {
22+
vector<string> words;
23+
int i = str.find_first_not_of(' ');
24+
int j = str.find_first_of(' ', i);
25+
while (j != string::npos) {
26+
words.emplace_back(str.substr(i, j - i));
27+
i = str.find_first_not_of(' ', j);
28+
j = str.find_first_of(' ', i);
29+
}
30+
words.emplace_back(str.substr(i));
31+
return words;
32+
}
33+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func areSentencesSimilar(sentence1 string, sentence2 string) bool {
2+
if sentence1 == sentence2 {
3+
return true
4+
}
5+
l1, l2 := len(sentence1), len(sentence2)
6+
if l1 == l2 {
7+
return false
8+
}
9+
if l1 < l2 {
10+
sentence1, sentence2 = sentence2, sentence1
11+
}
12+
i, j := 0, 0
13+
w1, w2 := strings.Fields(sentence1), strings.Fields(sentence2)
14+
l1, l2 = len(w1), len(w2)
15+
for i < l2 && w1[i] == w2[i] {
16+
i++
17+
}
18+
if i == l2 {
19+
return true
20+
}
21+
for j < l2 && w1[l1-1-j] == w2[l2-1-j] {
22+
j++
23+
}
24+
return j == l2 || i+j == l2
25+
}
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution {
22
public boolean areSentencesSimilar(String sentence1, String sentence2) {
3-
if (Objects.equals(sentence1, sentence2)) {
3+
if (sentence1.equals(sentence2)) {
44
return true;
55
}
66
int n1 = sentence1.length(), n2 = sentence2.length();
@@ -15,18 +15,17 @@ public boolean areSentencesSimilar(String sentence1, String sentence2) {
1515
String[] words1 = sentence1.split(" ");
1616
String[] words2 = sentence2.split(" ");
1717
int i = 0, j = 0;
18-
while (i < words2.length && Objects.equals(words1[i], words2[i])) {
18+
n1 = words1.length;
19+
n2 = words2.length;
20+
while (i < n2 && words1[i].equals(words2[i])) {
1921
++i;
2022
}
21-
if (i == words2.length) {
23+
if (i == n2) {
2224
return true;
2325
}
24-
while (j < words2.length && Objects.equals(words1[words1.length - 1 - j], words2[words2.length - 1 - j])) {
26+
while (j < n2 && words1[n1 - 1 - j].equals(words2[n2 - 1 - j])) {
2527
++j;
2628
}
27-
if (j == words2.length) {
28-
return true;
29-
}
30-
return i + j == words2.length;
29+
return j == n2 || i + j == n2;
3130
}
32-
}
31+
}

‎solution/1800-1899/1813.Sentence Similarity III/Solution.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ def areSentencesSimilar(self, sentence1: str, sentence2: str) -> bool:
99
sentence1, sentence2 = sentence2, sentence1
1010
words1, words2 = sentence1.split(), sentence2.split()
1111
i = j = 0
12-
while i < len(words2) and words1[i] == words2[i]:
12+
n1, n2 = len(words1), len(words2)
13+
while i < n2 and words1[i] == words2[i]:
1314
i += 1
14-
if i == len(words2):
15+
if i == n2:
1516
return True
16-
while j < len(words2) and words1[len(words1) - 1 - j] == words2[len(words2) - 1 - j]:
17+
while j < n2 and words1[n1 - 1 - j] == words2[n2 - 1 - j]:
1718
j += 1
18-
if j == len(words2):
19-
return True
20-
return i + j == len(words2)
19+
return j == n2 or i + j == n2

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /