From 1c8587907db49896b8b78da08949c655296a7201 Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 7 Oct 2024 23:39:15 +0300 Subject: [PATCH] feat: update solutions to lc problem: No.1813 --- .../1813.Sentence Similarity III/README.md | 39 +++++++++++++------ .../1813.Sentence Similarity III/README_EN.md | 39 +++++++++++++------ .../1813.Sentence Similarity III/Solution.js | 14 +++++++ .../1813.Sentence Similarity III/Solution.ts | 22 +++++------ 4 files changed, 80 insertions(+), 34 deletions(-) create mode 100644 solution/1800-1899/1813.Sentence Similarity III/Solution.js diff --git a/solution/1800-1899/1813.Sentence Similarity III/README.md b/solution/1800-1899/1813.Sentence Similarity III/README.md index febfb38aa3db9..9e2f0bae28e2f 100644 --- a/solution/1800-1899/1813.Sentence Similarity III/README.md +++ b/solution/1800-1899/1813.Sentence Similarity III/README.md @@ -192,20 +192,37 @@ func areSentencesSimilar(sentence1 string, sentence2 string) bool { ```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 [words1, words2] = [sentence1.split(' '), sentence2.split(' ')]; const [m, n] = [words1.length, words2.length]; - let [i, j] = [0, 0]; - while (i < n && words1[i] === words2[i]) { - ++i; + + if (m > n) return areSentencesSimilar(sentence2, sentence1); + + let [l, r] = [0, 0]; + for (let i = 0; i < n; i++) { + if (l === i && words1[i] === words2[i]) l++; + if (r === i && words2[n - i - 1] === words1[m - r - 1]) r++; } - while (j < n && words1[m - 1 - j] === words2[n - 1 - j]) { - ++j; + + return l + r >= m; +} +``` + +#### JavaScript + +```js +function areSentencesSimilar(sentence1, sentence2) { + const [words1, words2] = [sentence1.split(' '), sentence2.split(' ')]; + const [m, n] = [words1.length, words2.length]; + + if (m > n) return areSentencesSimilar(sentence2, sentence1); + + let [l, r] = [0, 0]; + for (let i = 0; i < n; i++) { + if (l === i && words1[i] === words2[i]) l++; + if (r === i && words2[n - i - 1] === words1[m - r - 1]) r++; } - return i + j >= n; + + return l + r >= m; } ``` 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 c12ddb9e1897f..eccea0127bc0f 100644 --- a/solution/1800-1899/1813.Sentence Similarity III/README_EN.md +++ b/solution/1800-1899/1813.Sentence Similarity III/README_EN.md @@ -198,20 +198,37 @@ func areSentencesSimilar(sentence1 string, sentence2 string) bool { ```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 [words1, words2] = [sentence1.split(' '), sentence2.split(' ')]; const [m, n] = [words1.length, words2.length]; - let [i, j] = [0, 0]; - while (i < n && words1[i] === words2[i]) { - ++i; + + if (m > n) return areSentencesSimilar(sentence2, sentence1); + + let [l, r] = [0, 0]; + for (let i = 0; i < n; i++) { + if (l === i && words1[i] === words2[i]) l++; + if (r === i && words2[n - i - 1] === words1[m - r - 1]) r++; } - while (j < n && words1[m - 1 - j] === words2[n - 1 - j]) { - ++j; + + return l + r >= m; +} +``` + +#### JavaScript + +```js +function areSentencesSimilar(sentence1, sentence2) { + const [words1, words2] = [sentence1.split(' '), sentence2.split(' ')]; + const [m, n] = [words1.length, words2.length]; + + if (m > n) return areSentencesSimilar(sentence2, sentence1); + + let [l, r] = [0, 0]; + for (let i = 0; i < n; i++) { + if (l === i && words1[i] === words2[i]) l++; + if (r === i && words2[n - i - 1] === words1[m - r - 1]) r++; } - return i + j >= n; + + return l + r >= m; } ``` diff --git a/solution/1800-1899/1813.Sentence Similarity III/Solution.js b/solution/1800-1899/1813.Sentence Similarity III/Solution.js new file mode 100644 index 0000000000000..82756f5890c7f --- /dev/null +++ b/solution/1800-1899/1813.Sentence Similarity III/Solution.js @@ -0,0 +1,14 @@ +function areSentencesSimilar(sentence1, sentence2) { + const [words1, words2] = [sentence1.split(' '), sentence2.split(' ')]; + const [m, n] = [words1.length, words2.length]; + + if (m > n) return areSentencesSimilar(sentence2, sentence1); + + let [l, r] = [0, 0]; + for (let i = 0; i < n; i++) { + if (l === i && words1[i] === words2[i]) l++; + if (r === i && words2[n - i - 1] === words1[m - r - 1]) r++; + } + + return l + r >= m; +} diff --git a/solution/1800-1899/1813.Sentence Similarity III/Solution.ts b/solution/1800-1899/1813.Sentence Similarity III/Solution.ts index b5cd4659c1634..23edff4cdd51e 100644 --- a/solution/1800-1899/1813.Sentence Similarity III/Solution.ts +++ b/solution/1800-1899/1813.Sentence Similarity III/Solution.ts @@ -1,16 +1,14 @@ 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 [words1, words2] = [sentence1.split(' '), sentence2.split(' ')]; 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; + + if (m > n) return areSentencesSimilar(sentence2, sentence1); + + let [l, r] = [0, 0]; + for (let i = 0; i < n; i++) { + if (l === i && words1[i] === words2[i]) l++; + if (r === i && words2[n - i - 1] === words1[m - r - 1]) r++; } - return i + j >= n; + + return l + r >= m; }