From 34102cfc9e95217ddf1b8585ba2b57e8940b0441 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Sat, 7 Oct 2023 09:44:14 +0000 Subject: [PATCH] feat: add solutions to lc problems: No.1813,1814,1816 * No.1813.Sentence Similarity III * No.1814.Count Nice Pairs in an Array * No.1816.Truncate Sentence --- .../1813.Sentence Similarity III/README.md | 21 ++++++++++++ .../1813.Sentence Similarity III/README_EN.md | 31 +++++++++++++++++ .../1813.Sentence Similarity III/Solution.ts | 16 +++++++++ .../README.md | 24 +++++++++++++ .../README_EN.md | 34 +++++++++++++++++++ .../Solution.ts | 19 +++++++++++ .../1816.Truncate Sentence/README.md | 17 ++++++++-- .../1816.Truncate Sentence/README_EN.md | 23 ++++++++++++- .../1816.Truncate Sentence/Solution.js | 2 +- .../1816.Truncate Sentence/Solution.ts | 8 +++++ 10 files changed, 191 insertions(+), 4 deletions(-) create mode 100644 solution/1800-1899/1813.Sentence Similarity III/Solution.ts create mode 100644 solution/1800-1899/1814.Count Nice Pairs in an Array/Solution.ts create mode 100644 solution/1800-1899/1816.Truncate Sentence/Solution.ts diff --git a/solution/1800-1899/1813.Sentence Similarity III/README.md b/solution/1800-1899/1813.Sentence Similarity III/README.md index b332491be86f0..2e6f17b98871c 100644 --- a/solution/1800-1899/1813.Sentence Similarity III/README.md +++ b/solution/1800-1899/1813.Sentence Similarity III/README.md @@ -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; +} +``` + ### **...** ``` diff --git a/solution/1800-1899/1813.Sentence Similarity III/README_EN.md b/solution/1800-1899/1813.Sentence Similarity III/README_EN.md index 282c166cf9ed9..fc79dcd1d8981 100644 --- a/solution/1800-1899/1813.Sentence Similarity III/README_EN.md +++ b/solution/1800-1899/1813.Sentence Similarity III/README_EN.md @@ -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. + ### **Python3** @@ -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; +} +``` + ### **...** ``` diff --git a/solution/1800-1899/1813.Sentence Similarity III/Solution.ts b/solution/1800-1899/1813.Sentence Similarity III/Solution.ts new file mode 100644 index 0000000000000..b5cd4659c1634 --- /dev/null +++ b/solution/1800-1899/1813.Sentence Similarity III/Solution.ts @@ -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; +} diff --git a/solution/1800-1899/1814.Count Nice Pairs in an Array/README.md b/solution/1800-1899/1814.Count Nice Pairs in an Array/README.md index 50a27bd1ec423..3fe311242d8fd 100644 --- a/solution/1800-1899/1814.Count Nice Pairs in an Array/README.md +++ b/solution/1800-1899/1814.Count Nice Pairs in an Array/README.md @@ -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(); + 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; +} +``` + ### **...** ``` diff --git a/solution/1800-1899/1814.Count Nice Pairs in an Array/README_EN.md b/solution/1800-1899/1814.Count Nice Pairs in an Array/README_EN.md index ea40c509f72ba..3f4a53da9142d 100644 --- a/solution/1800-1899/1814.Count Nice Pairs in an Array/README_EN.md +++ b/solution/1800-1899/1814.Count Nice Pairs in an Array/README_EN.md @@ -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)$. + ### **Python3** @@ -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(); + 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; +} +``` + ### **...** ``` diff --git a/solution/1800-1899/1814.Count Nice Pairs in an Array/Solution.ts b/solution/1800-1899/1814.Count Nice Pairs in an Array/Solution.ts new file mode 100644 index 0000000000000..a4afac324279c --- /dev/null +++ b/solution/1800-1899/1814.Count Nice Pairs in an Array/Solution.ts @@ -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(); + 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; +} diff --git a/solution/1800-1899/1816.Truncate Sentence/README.md b/solution/1800-1899/1816.Truncate Sentence/README.md index f9788aacf967a..3ca8c4e594a1e 100644 --- a/solution/1800-1899/1816.Truncate Sentence/README.md +++ b/solution/1800-1899/1816.Truncate Sentence/README.md @@ -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$ 即可。 @@ -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 @@ -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); } } diff --git a/solution/1800-1899/1816.Truncate Sentence/README_EN.md b/solution/1800-1899/1816.Truncate Sentence/README_EN.md index ac71698978598..b110b8c3f1fa5 100644 --- a/solution/1800-1899/1816.Truncate Sentence/README_EN.md +++ b/solution/1800-1899/1816.Truncate Sentence/README_EN.md @@ -54,6 +54,14 @@ Hence, you should return "What is the solution". ## 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)$. + ### **Python3** @@ -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 @@ -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); } } diff --git a/solution/1800-1899/1816.Truncate Sentence/Solution.js b/solution/1800-1899/1816.Truncate Sentence/Solution.js index 01e75853fe39c..0387a624e1b78 100644 --- a/solution/1800-1899/1816.Truncate Sentence/Solution.js +++ b/solution/1800-1899/1816.Truncate Sentence/Solution.js @@ -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); } } diff --git a/solution/1800-1899/1816.Truncate Sentence/Solution.ts b/solution/1800-1899/1816.Truncate Sentence/Solution.ts new file mode 100644 index 0000000000000..d9caca7ac4d1b --- /dev/null +++ b/solution/1800-1899/1816.Truncate Sentence/Solution.ts @@ -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; +}