From a21b1e8eb4b3ce85bb30c47467693ddf15cc1b37 Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Mon, 23 Sep 2024 11:37:18 +0530 Subject: [PATCH 01/13] Create Solution.js --- solution/Solution.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 solution/Solution.js diff --git a/solution/Solution.js b/solution/Solution.js new file mode 100644 index 0000000000000..bc6eefb4efe2b --- /dev/null +++ b/solution/Solution.js @@ -0,0 +1,19 @@ +/** + * @param {string} s + * @param {string[]} dictionary + * @return {number} + */ +var minExtraChar = function(s, dictionary) { + const ss = new Set(dictionary); + const n = s.length; + const f = new Array(n + 1).fill(0); + for (let i = 1; i <= n; ++i) { + f[i] = f[i - 1] + 1; + for (let j = 0; j < i; ++j) { + if (ss.has(s.substring(j, i))) { + f[i] = Math.min(f[i], f[j]); + } + } + } + return f[n]; +}; From 324cc2b6c4d59e6dba76c3db790ecbd91ad0dafd Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Mon, 23 Sep 2024 11:40:57 +0530 Subject: [PATCH 02/13] Create Solution.js --- .../Solution.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 solution/2700-2799/2707.Extra Characters in a String/Solution.js diff --git a/solution/2700-2799/2707.Extra Characters in a String/Solution.js b/solution/2700-2799/2707.Extra Characters in a String/Solution.js new file mode 100644 index 0000000000000..bc6eefb4efe2b --- /dev/null +++ b/solution/2700-2799/2707.Extra Characters in a String/Solution.js @@ -0,0 +1,19 @@ +/** + * @param {string} s + * @param {string[]} dictionary + * @return {number} + */ +var minExtraChar = function(s, dictionary) { + const ss = new Set(dictionary); + const n = s.length; + const f = new Array(n + 1).fill(0); + for (let i = 1; i <= n; ++i) { + f[i] = f[i - 1] + 1; + for (let j = 0; j < i; ++j) { + if (ss.has(s.substring(j, i))) { + f[i] = Math.min(f[i], f[j]); + } + } + } + return f[n]; +}; From 80634a89a3aca30e6204332eab638bf0eff2362c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 08:22:47 +0800 Subject: [PATCH 03/13] Update Solution.js --- .../2707.Extra Characters in a String/Solution.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/solution/2700-2799/2707.Extra Characters in a String/Solution.js b/solution/2700-2799/2707.Extra Characters in a String/Solution.js index bc6eefb4efe2b..078d05aab4f5f 100644 --- a/solution/2700-2799/2707.Extra Characters in a String/Solution.js +++ b/solution/2700-2799/2707.Extra Characters in a String/Solution.js @@ -3,17 +3,17 @@ * @param {string[]} dictionary * @return {number} */ -var minExtraChar = function(s, dictionary) { +var minExtraChar = function (s, dictionary) { const ss = new Set(dictionary); const n = s.length; - const f = new Array(n + 1).fill(0); + const f = Array(n + 1).fill(0); for (let i = 1; i <= n; ++i) { f[i] = f[i - 1] + 1; for (let j = 0; j < i; ++j) { - if (ss.has(s.substring(j, i))) { + if (ss.has(s.slice(j, i))) { f[i] = Math.min(f[i], f[j]); } } } - return f[n]; + return f[n]; }; From 3680e8bf99babad73e9bac11b67764332e375a28 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 08:23:05 +0800 Subject: [PATCH 04/13] Delete solution/Solution.js --- solution/Solution.js | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 solution/Solution.js diff --git a/solution/Solution.js b/solution/Solution.js deleted file mode 100644 index bc6eefb4efe2b..0000000000000 --- a/solution/Solution.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * @param {string} s - * @param {string[]} dictionary - * @return {number} - */ -var minExtraChar = function(s, dictionary) { - const ss = new Set(dictionary); - const n = s.length; - const f = new Array(n + 1).fill(0); - for (let i = 1; i <= n; ++i) { - f[i] = f[i - 1] + 1; - for (let j = 0; j < i; ++j) { - if (ss.has(s.substring(j, i))) { - f[i] = Math.min(f[i], f[j]); - } - } - } - return f[n]; -}; From 7a181216edc3dce766dbe4b95f2dd024cdfefdc1 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 08:23:49 +0800 Subject: [PATCH 05/13] Update README.md --- .../README.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/solution/2700-2799/2707.Extra Characters in a String/README.md b/solution/2700-2799/2707.Extra Characters in a String/README.md index b2cff7fea73de..825e7da5463e6 100644 --- a/solution/2700-2799/2707.Extra Characters in a String/README.md +++ b/solution/2700-2799/2707.Extra Characters in a String/README.md @@ -210,6 +210,30 @@ impl Solution { } ``` +#### JavaScript + +```js +/** + * @param {string} s + * @param {string[]} dictionary + * @return {number} + */ +var minExtraChar = function (s, dictionary) { + const ss = new Set(dictionary); + const n = s.length; + const f = Array(n + 1).fill(0); + for (let i = 1; i <= n; ++i) { + f[i] = f[i - 1] + 1; + for (let j = 0; j < i; ++j) { + if (ss.has(s.slice(j, i))) { + f[i] = Math.min(f[i], f[j]); + } + } + } + return f[n]; +}; +``` + From 46a2880377f0b6d4f0b9ec021893456cf4b14a80 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 08:24:08 +0800 Subject: [PATCH 06/13] Update README_EN.md --- .../README_EN.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/solution/2700-2799/2707.Extra Characters in a String/README_EN.md b/solution/2700-2799/2707.Extra Characters in a String/README_EN.md index 13eb8c12b9d59..777bf14ca7383 100644 --- a/solution/2700-2799/2707.Extra Characters in a String/README_EN.md +++ b/solution/2700-2799/2707.Extra Characters in a String/README_EN.md @@ -211,6 +211,30 @@ impl Solution { } ``` +#### JavaScript + +```js +/** + * @param {string} s + * @param {string[]} dictionary + * @return {number} + */ +var minExtraChar = function (s, dictionary) { + const ss = new Set(dictionary); + const n = s.length; + const f = Array(n + 1).fill(0); + for (let i = 1; i <= n; ++i) { + f[i] = f[i - 1] + 1; + for (let j = 0; j < i; ++j) { + if (ss.has(s.slice(j, i))) { + f[i] = Math.min(f[i], f[j]); + } + } + } + return f[n]; +}; +``` + From f4825dbb3d698ef1c9e47f2c7cc8a5f861fb0d93 Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Tue, 24 Sep 2024 10:34:48 +0530 Subject: [PATCH 07/13] Create Solution.js --- .../Solution.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js new file mode 100644 index 0000000000000..301d9fdbc553b --- /dev/null +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js @@ -0,0 +1,24 @@ +var longestCommonPrefix = function(arr1, arr2) { + let set = new Set(); + + for (let x of arr1) { + while (x > 0) { + set.add(x); + x = Math.floor(x / 10); + } + } + + let ans = 0; + + for (let x of arr2) { + while (x > 0) { + if (set.has(x)) { + ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); + break; + } + x = Math.floor(x / 10); + } + } + + return ans; +}; From 8c24319aace1c1d40bacdb113b95b5af884ccded Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Tue, 24 Sep 2024 10:36:25 +0530 Subject: [PATCH 08/13] Update Solution.ts --- .../Solution.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts index 88c02857fb7fe..153db70971f04 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts @@ -1,17 +1,24 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number { - const s: Set = new Set(); + let set = new Set(); + for (let x of arr1) { - for (; x; x = (x / 10) | 0) { - s.add(x % 10); + while (x > 0) { + set.add(x); + x = Math.floor(x / 10); } } - let ans: number = 0; + + let ans = 0; + for (let x of arr2) { - for (; x; x = (x / 10) | 0) { - if (s.has(x % 10)) { + while (x > 0) { + if (set.has(x)) { ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); + break; } + x = Math.floor(x / 10); } } + return ans; } From 9223b1f51bdaf11619b29c3ea418d858c96cf089 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 24 Sep 2024 10:44:29 +0000 Subject: [PATCH 09/13] style: format code and docs with prettier --- .../Solution.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js index 301d9fdbc553b..abae6f729d795 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js @@ -1,4 +1,4 @@ -var longestCommonPrefix = function(arr1, arr2) { +var longestCommonPrefix = function (arr1, arr2) { let set = new Set(); for (let x of arr1) { From fa6703870dcd493e874b5289713805c03437a00f Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 18:48:02 +0800 Subject: [PATCH 10/13] Update Solution.ts --- .../Solution.ts | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts index 153db70971f04..f66c7309c52b2 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts @@ -1,24 +1,17 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number { - let set = new Set(); - + const s: Set = new Set(); for (let x of arr1) { - while (x > 0) { - set.add(x); - x = Math.floor(x / 10); + for (; x; x = Math.floor(x / 10)) { + s.add(x); } } - - let ans = 0; - + let ans: number = 0; for (let x of arr2) { - while (x > 0) { - if (set.has(x)) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); - break; } - x = Math.floor(x / 10); } } - return ans; } From cc15596492c4ed4f41c52cd79059f7e510b863c5 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 18:48:56 +0800 Subject: [PATCH 11/13] Update Solution.js --- .../Solution.js | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js index abae6f729d795..86762c5539377 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js @@ -1,24 +1,22 @@ +/** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number} + */ var longestCommonPrefix = function (arr1, arr2) { - let set = new Set(); - + const s = new Set(); for (let x of arr1) { - while (x > 0) { - set.add(x); - x = Math.floor(x / 10); + for (; x; x = Math.floor(x / 10)) { + s.add(x); } } - let ans = 0; - for (let x of arr2) { - while (x > 0) { - if (set.has(x)) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); - break; } - x = Math.floor(x / 10); } } - return ans; }; From 313f6b500334b0cb6ec37b220508b714ab982f4b Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 18:50:05 +0800 Subject: [PATCH 12/13] Update README.md --- .../README.md | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md index b4d7746119489..a501e904cf3be 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md @@ -176,14 +176,14 @@ func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) { function longestCommonPrefix(arr1: number[], arr2: number[]): number { const s: Set = new Set(); for (let x of arr1) { - for (; x; x = (x / 10) | 0) { - s.add(x % 10); + for (; x; x = Math.floor(x / 10)) { + s.add(x); } } let ans: number = 0; for (let x of arr2) { - for (; x; x = (x / 10) | 0) { - if (s.has(x % 10)) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); } } @@ -192,6 +192,33 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number { } ``` +#### JavaScript + +```js +/** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number} + */ +var longestCommonPrefix = function (arr1, arr2) { + const s = new Set(); + for (let x of arr1) { + for (; x; x = Math.floor(x / 10)) { + s.add(x); + } + } + let ans = 0; + for (let x of arr2) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { + ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); + } + } + } + return ans; +}; +``` + From 63ef10d701f1f4ab872127ddb36361987ac285d4 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 18:50:17 +0800 Subject: [PATCH 13/13] Update README_EN.md --- .../README_EN.md | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md index 1ff77d43c30de..6dab65a426ad3 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md @@ -174,14 +174,14 @@ func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) { function longestCommonPrefix(arr1: number[], arr2: number[]): number { const s: Set = new Set(); for (let x of arr1) { - for (; x; x = (x / 10) | 0) { - s.add(x % 10); + for (; x; x = Math.floor(x / 10)) { + s.add(x); } } let ans: number = 0; for (let x of arr2) { - for (; x; x = (x / 10) | 0) { - if (s.has(x % 10)) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); } } @@ -190,6 +190,33 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number { } ``` +#### JavaScript + +```js +/** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number} + */ +var longestCommonPrefix = function (arr1, arr2) { + const s = new Set(); + for (let x of arr1) { + for (; x; x = Math.floor(x / 10)) { + s.add(x); + } + } + let ans = 0; + for (let x of arr2) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { + ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); + } + } + } + return ans; +}; +``` +