Skip to content

Commit d1b1de9

Browse files
authored
feat: add solutions to lc problems: No.1813,1814,1816 (#1758)
1 parent f847c0f commit d1b1de9

File tree

10 files changed

+191
-4
lines changed

10 files changed

+191
-4
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,27 @@ func areSentencesSimilar(sentence1 string, sentence2 string) bool {
168168
}
169169
```
170170

171+
### **TypeScript**
172+
173+
```ts
174+
function areSentencesSimilar(sentence1: string, sentence2: string): boolean {
175+
const words1 = sentence1.split(' ');
176+
const words2 = sentence2.split(' ');
177+
if (words1.length < words2.length) {
178+
return areSentencesSimilar(sentence2, sentence1);
179+
}
180+
const [m, n] = [words1.length, words2.length];
181+
let [i, j] = [0, 0];
182+
while (i < n && words1[i] === words2[i]) {
183+
++i;
184+
}
185+
while (j < n && words1[m - 1 - j] === words2[n - 1 - j]) {
186+
++j;
187+
}
188+
return i + j >= n;
189+
}
190+
```
191+
171192
### **...**
172193

173194
```

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

+31
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646

4747
## Solutions
4848

49+
**Solution 1: Two Pointers**
50+
51+
We split the two sentences into two word arrays `words1` and `words2` by spaces. Let the lengths of `words1` and `words2` be $m$ and $n$, respectively, and assume that $m \ge nn.
52+
53+
We use two pointers $i$ and $j$, initially $i = j = 0$. Next, we loop to check whether `words1[i]` is equal to `words2[i]`, and if so, pointer $i$ continues to move right; then we loop to check whether `words1[m - 1 - j]` is equal to `words2[n - 1 - j]`, and if so, pointer $j$ continues to move right.
54+
55+
After the loop, if $i + j \ge n$, it means that the two sentences are similar, and we return `true`; otherwise, we return `false`.
56+
57+
The time complexity is $O(L)$, and the space complexity is $O(L)$, where $L$ is the sum of the lengths of the two sentences.
58+
4959
<!-- tabs:start -->
5060

5161
### **Python3**
@@ -145,6 +155,27 @@ func areSentencesSimilar(sentence1 string, sentence2 string) bool {
145155
}
146156
```
147157

158+
### **TypeScript**
159+
160+
```ts
161+
function areSentencesSimilar(sentence1: string, sentence2: string): boolean {
162+
const words1 = sentence1.split(' ');
163+
const words2 = sentence2.split(' ');
164+
if (words1.length < words2.length) {
165+
return areSentencesSimilar(sentence2, sentence1);
166+
}
167+
const [m, n] = [words1.length, words2.length];
168+
let [i, j] = [0, 0];
169+
while (i < n && words1[i] === words2[i]) {
170+
++i;
171+
}
172+
while (j < n && words1[m - 1 - j] === words2[n - 1 - j]) {
173+
++j;
174+
}
175+
return i + j >= n;
176+
}
177+
```
178+
148179
### **...**
149180

150181
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function areSentencesSimilar(sentence1: string, sentence2: string): boolean {
2+
const words1 = sentence1.split(' ');
3+
const words2 = sentence2.split(' ');
4+
if (words1.length < words2.length) {
5+
return areSentencesSimilar(sentence2, sentence1);
6+
}
7+
const [m, n] = [words1.length, words2.length];
8+
let [i, j] = [0, 0];
9+
while (i < n && words1[i] === words2[i]) {
10+
++i;
11+
}
12+
while (j < n && words1[m - 1 - j] === words2[n - 1 - j]) {
13+
++j;
14+
}
15+
return i + j >= n;
16+
}

solution/1800-1899/1814.Count Nice Pairs in an Array/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,30 @@ var countNicePairs = function (nums) {
298298
};
299299
```
300300

301+
### **TypeScript**
302+
303+
```ts
304+
function countNicePairs(nums: number[]): number {
305+
const rev = (x: number): number => {
306+
let y = 0;
307+
while (x) {
308+
y = y * 10 + (x % 10);
309+
x = Math.floor(x / 10);
310+
}
311+
return y;
312+
};
313+
const mod = 10 ** 9 + 7;
314+
const cnt = new Map<number, number>();
315+
let ans = 0;
316+
for (const x of nums) {
317+
const y = x - rev(x);
318+
ans = (ans + (cnt.get(y) ?? 0)) % mod;
319+
cnt.set(y, (cnt.get(y) ?? 0) + 1);
320+
}
321+
return ans;
322+
}
323+
```
324+
301325
### **...**
302326

303327
```

solution/1800-1899/1814.Count Nice Pairs in an Array/README_EN.md

+34
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@
4141

4242
## Solutions
4343

44+
**Method 1: Equation Transformation + Hash Table**
45+
46+
For the index pair $(i, j)$, if it satisfies the condition, then we have $nums[i] + rev(nums[j]) = nums[j] + rev(nums[i])$, which means $nums[i] - nums[j] = rev(nums[j]) - rev(nums[i])$.
47+
48+
Therefore, we can use $nums[i] - rev(nums[i])$ as the key of a hash table and count the number of occurrences of each key. Finally, we calculate the combination of values corresponding to each key, add them up, and get the final answer.
49+
50+
Note that we need to perform modulo operation on the answer.
51+
52+
The time complexity is $O(n \times \log M)$, where $n$ and $M$ are the length of the $nums$ array and the maximum value in the $nums$ array, respectively. The space complexity is $O(n)$.
53+
4454
<!-- tabs:start -->
4555

4656
### **Python3**
@@ -280,6 +290,30 @@ var countNicePairs = function (nums) {
280290
};
281291
```
282292

293+
### **TypeScript**
294+
295+
```ts
296+
function countNicePairs(nums: number[]): number {
297+
const rev = (x: number): number => {
298+
let y = 0;
299+
while (x) {
300+
y = y * 10 + (x % 10);
301+
x = Math.floor(x / 10);
302+
}
303+
return y;
304+
};
305+
const mod = 10 ** 9 + 7;
306+
const cnt = new Map<number, number>();
307+
let ans = 0;
308+
for (const x of nums) {
309+
const y = x - rev(x);
310+
ans = (ans + (cnt.get(y) ?? 0)) % mod;
311+
cnt.set(y, (cnt.get(y) ?? 0) + 1);
312+
}
313+
return ans;
314+
}
315+
```
316+
283317
### **...**
284318

285319
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function countNicePairs(nums: number[]): number {
2+
const rev = (x: number): number => {
3+
let y = 0;
4+
while (x) {
5+
y = y * 10 + (x % 10);
6+
x = Math.floor(x / 10);
7+
}
8+
return y;
9+
};
10+
const mod = 10 ** 9 + 7;
11+
const cnt = new Map<number, number>();
12+
let ans = 0;
13+
for (const x of nums) {
14+
const y = x - rev(x);
15+
ans = (ans + (cnt.get(y) ?? 0)) % mod;
16+
cnt.set(y, (cnt.get(y) ?? 0) + 1);
17+
}
18+
return ans;
19+
}

solution/1800-1899/1816.Truncate Sentence/README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ s 中的单词为 ["What", "is" "the", "solution", "to", "this", "problem"]
5959

6060
**方法一:模拟**
6161

62-
我们从前往后遍历字符串 $s$,对于当前遍历到的字符 $s[i]$,如果 $s[i]$ 是空格,那么 $k$ 自减 1,当 $k$ 为 0 时,说明已经截取了 $k$ 个单词,截取字符串 $s[0:i]$ 返回即可。
62+
我们从前往后遍历字符串 $s$,对于当前遍历到的字符 $s[i]$,如果 $s[i]$ 是空格,那么 $k$ 自减 $1$,当 $k$ 为 $0$ 时,说明已经截取了 $k$ 个单词,截取字符串 $s[0..i)$ 返回即可。
6363

6464
遍历结束,返回 $s$ 即可。
6565

@@ -136,6 +136,19 @@ func truncateSentence(s string, k int) string {
136136
}
137137
```
138138

139+
### **TypeScript**
140+
141+
```ts
142+
function truncateSentence(s: string, k: number): string {
143+
for (let i = 0; i < s.length; ++i) {
144+
if (s[i] === ' ' && --k === 0) {
145+
return s.slice(0, i);
146+
}
147+
}
148+
return s;
149+
}
150+
```
151+
139152
### **JavaScript**
140153

141154
```js
@@ -146,7 +159,7 @@ func truncateSentence(s string, k int) string {
146159
*/
147160
var truncateSentence = function (s, k) {
148161
for (let i = 0; i < s.length; ++i) {
149-
if (s[i] == ' ' && --k == 0) {
162+
if (s[i] === ' ' && --k === 0) {
150163
return s.slice(0, i);
151164
}
152165
}

solution/1800-1899/1816.Truncate Sentence/README_EN.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ Hence, you should return &quot;What is the solution&quot;.</pre>
5454

5555
## Solutions
5656

57+
**Method 1: Simulation**
58+
59+
We traverse the string $s$ from the beginning. For the current character $s[i]$, if it is a space, we decrement $k$. When $k$ becomes $0$, it means that we have extracted $k$ words, so we return the substring $s[0..i)$.
60+
61+
After the traversal, we return $s$.
62+
63+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. Ignoring the space complexity of the answer, the space complexity is $O(1)$.
64+
5765
<!-- tabs:start -->
5866

5967
### **Python3**
@@ -121,6 +129,19 @@ func truncateSentence(s string, k int) string {
121129
}
122130
```
123131

132+
### **TypeScript**
133+
134+
```ts
135+
function truncateSentence(s: string, k: number): string {
136+
for (let i = 0; i < s.length; ++i) {
137+
if (s[i] === ' ' && --k === 0) {
138+
return s.slice(0, i);
139+
}
140+
}
141+
return s;
142+
}
143+
```
144+
124145
### **JavaScript**
125146

126147
```js
@@ -131,7 +152,7 @@ func truncateSentence(s string, k int) string {
131152
*/
132153
var truncateSentence = function (s, k) {
133154
for (let i = 0; i < s.length; ++i) {
134-
if (s[i] == ' ' && --k == 0) {
155+
if (s[i] === ' ' && --k === 0) {
135156
return s.slice(0, i);
136157
}
137158
}

solution/1800-1899/1816.Truncate Sentence/Solution.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
var truncateSentence = function (s, k) {
77
for (let i = 0; i < s.length; ++i) {
8-
if (s[i] == ' ' && --k == 0) {
8+
if (s[i] === ' ' && --k === 0) {
99
return s.slice(0, i);
1010
}
1111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function truncateSentence(s: string, k: number): string {
2+
for (let i = 0; i < s.length; ++i) {
3+
if (s[i] === ' ' && --k === 0) {
4+
return s.slice(0, i);
5+
}
6+
}
7+
return s;
8+
}

0 commit comments

Comments
 (0)