Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.1813,1814,1816 #1758

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions solution/1800-1899/1813.Sentence Similarity III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,27 @@ func areSentencesSimilar(sentence1 string, sentence2 string) bool {
}
```

### **TypeScript**

```ts
function areSentencesSimilar(sentence1: string, sentence2: string): boolean {
const words1 = sentence1.split(' ');
const words2 = sentence2.split(' ');
if (words1.length < words2.length) {
return areSentencesSimilar(sentence2, sentence1);
}
const [m, n] = [words1.length, words2.length];
let [i, j] = [0, 0];
while (i < n && words1[i] === words2[i]) {
++i;
}
while (j < n && words1[m - 1 - j] === words2[n - 1 - j]) {
++j;
}
return i + j >= n;
}
```

### **...**

```
Expand Down
31 changes: 31 additions & 0 deletions solution/1800-1899/1813.Sentence Similarity III/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@

## Solutions

**Solution 1: Two Pointers**

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.

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.

After the loop, if $i + j \ge n$, it means that the two sentences are similar, and we return `true`; otherwise, we return `false`.

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.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -145,6 +155,27 @@ func areSentencesSimilar(sentence1 string, sentence2 string) bool {
}
```

### **TypeScript**

```ts
function areSentencesSimilar(sentence1: string, sentence2: string): boolean {
const words1 = sentence1.split(' ');
const words2 = sentence2.split(' ');
if (words1.length < words2.length) {
return areSentencesSimilar(sentence2, sentence1);
}
const [m, n] = [words1.length, words2.length];
let [i, j] = [0, 0];
while (i < n && words1[i] === words2[i]) {
++i;
}
while (j < n && words1[m - 1 - j] === words2[n - 1 - j]) {
++j;
}
return i + j >= n;
}
```

### **...**

```
Expand Down
16 changes: 16 additions & 0 deletions solution/1800-1899/1813.Sentence Similarity III/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function areSentencesSimilar(sentence1: string, sentence2: string): boolean {
const words1 = sentence1.split(' ');
const words2 = sentence2.split(' ');
if (words1.length < words2.length) {
return areSentencesSimilar(sentence2, sentence1);
}
const [m, n] = [words1.length, words2.length];
let [i, j] = [0, 0];
while (i < n && words1[i] === words2[i]) {
++i;
}
while (j < n && words1[m - 1 - j] === words2[n - 1 - j]) {
++j;
}
return i + j >= n;
}
24 changes: 24 additions & 0 deletions solution/1800-1899/1814.Count Nice Pairs in an Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,30 @@ var countNicePairs = function (nums) {
};
```

### **TypeScript**

```ts
function countNicePairs(nums: number[]): number {
const rev = (x: number): number => {
let y = 0;
while (x) {
y = y * 10 + (x % 10);
x = Math.floor(x / 10);
}
return y;
};
const mod = 10 ** 9 + 7;
const cnt = new Map<number, number>();
let ans = 0;
for (const x of nums) {
const y = x - rev(x);
ans = (ans + (cnt.get(y) ?? 0)) % mod;
cnt.set(y, (cnt.get(y) ?? 0) + 1);
}
return ans;
}
```

### **...**

```
Expand Down
34 changes: 34 additions & 0 deletions solution/1800-1899/1814.Count Nice Pairs in an Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@

## Solutions

**Method 1: Equation Transformation + Hash Table**

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])$.

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.

Note that we need to perform modulo operation on the answer.

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)$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -280,6 +290,30 @@ var countNicePairs = function (nums) {
};
```

### **TypeScript**

```ts
function countNicePairs(nums: number[]): number {
const rev = (x: number): number => {
let y = 0;
while (x) {
y = y * 10 + (x % 10);
x = Math.floor(x / 10);
}
return y;
};
const mod = 10 ** 9 + 7;
const cnt = new Map<number, number>();
let ans = 0;
for (const x of nums) {
const y = x - rev(x);
ans = (ans + (cnt.get(y) ?? 0)) % mod;
cnt.set(y, (cnt.get(y) ?? 0) + 1);
}
return ans;
}
```

### **...**

```
Expand Down
19 changes: 19 additions & 0 deletions solution/1800-1899/1814.Count Nice Pairs in an Array/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function countNicePairs(nums: number[]): number {
const rev = (x: number): number => {
let y = 0;
while (x) {
y = y * 10 + (x % 10);
x = Math.floor(x / 10);
}
return y;
};
const mod = 10 ** 9 + 7;
const cnt = new Map<number, number>();
let ans = 0;
for (const x of nums) {
const y = x - rev(x);
ans = (ans + (cnt.get(y) ?? 0)) % mod;
cnt.set(y, (cnt.get(y) ?? 0) + 1);
}
return ans;
}
17 changes: 15 additions & 2 deletions solution/1800-1899/1816.Truncate Sentence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ s 中的单词为 ["What", "is" "the", "solution", "to", "this", "problem"]

**方法一:模拟**

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

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

Expand Down Expand Up @@ -136,6 +136,19 @@ func truncateSentence(s string, k int) string {
}
```

### **TypeScript**

```ts
function truncateSentence(s: string, k: number): string {
for (let i = 0; i < s.length; ++i) {
if (s[i] === ' ' && --k === 0) {
return s.slice(0, i);
}
}
return s;
}
```

### **JavaScript**

```js
Expand All @@ -146,7 +159,7 @@ func truncateSentence(s string, k int) string {
*/
var truncateSentence = function (s, k) {
for (let i = 0; i < s.length; ++i) {
if (s[i] == ' ' && --k == 0) {
if (s[i] === ' ' && --k === 0) {
return s.slice(0, i);
}
}
Expand Down
23 changes: 22 additions & 1 deletion solution/1800-1899/1816.Truncate Sentence/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ Hence, you should return &quot;What is the solution&quot;.</pre>

## Solutions

**Method 1: Simulation**

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)$.

After the traversal, we return $s$.

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)$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -121,6 +129,19 @@ func truncateSentence(s string, k int) string {
}
```

### **TypeScript**

```ts
function truncateSentence(s: string, k: number): string {
for (let i = 0; i < s.length; ++i) {
if (s[i] === ' ' && --k === 0) {
return s.slice(0, i);
}
}
return s;
}
```

### **JavaScript**

```js
Expand All @@ -131,7 +152,7 @@ func truncateSentence(s string, k int) string {
*/
var truncateSentence = function (s, k) {
for (let i = 0; i < s.length; ++i) {
if (s[i] == ' ' && --k == 0) {
if (s[i] === ' ' && --k === 0) {
return s.slice(0, i);
}
}
Expand Down
2 changes: 1 addition & 1 deletion solution/1800-1899/1816.Truncate Sentence/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
var truncateSentence = function (s, k) {
for (let i = 0; i < s.length; ++i) {
if (s[i] == ' ' && --k == 0) {
if (s[i] === ' ' && --k === 0) {
return s.slice(0, i);
}
}
Expand Down
8 changes: 8 additions & 0 deletions solution/1800-1899/1816.Truncate Sentence/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function truncateSentence(s: string, k: number): string {
for (let i = 0; i < s.length; ++i) {
if (s[i] === ' ' && --k === 0) {
return s.slice(0, i);
}
}
return s;
}