From 2783ec55e78b8acb882a2f1bd5ade7029db9aa91 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 18 Feb 2024 17:38:43 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3043 No.3043.Find the Length of the Longest Common Prefix --- .../README.md | 101 +++++++++++++++++- .../README_EN.md | 101 +++++++++++++++++- .../Solution.cpp | 21 ++++ .../Solution.go | 17 +++ .../Solution.java | 20 ++++ .../Solution.py | 15 +++ .../Solution.ts | 17 +++ 7 files changed, 284 insertions(+), 8 deletions(-) create mode 100644 solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.cpp create mode 100644 solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.go create mode 100644 solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.java create mode 100644 solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.py create mode 100644 solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts 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 a00138baf4720..df38821043d9a 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 @@ -50,24 +50,117 @@ ## 解法 -### 方法一 +### 方法一:哈希表 + +我们可以使用哈希表来存储 `arr1` 中的所有数字的前缀,然后遍历 `arr2` 中的所有数字 $x$,对于每个数字 $x$,我们从最高位开始逐渐减小,判断是否存在于哈希表中,如果存在,那么我们就找到了一个公共前缀,此时更新答案即可。 + +时间复杂度 $O(m \times \log M + n \times \log N)$,空间复杂度 $O(m \times \log M)$。其中 $m$ 和 $n$ 分别是 `arr1` 和 `arr2` 的长度,而 $M$ 和 $N$ 分别是 `arr1` 和 `arr2` 中的最大值。 ```python - +class Solution: + def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int: + s = set() + for x in arr1: + while x: + s.add(x) + x //= 10 + ans = 0 + for x in arr2: + while x: + if x in s: + ans = max(ans, len(str(x))) + break + x //= 10 + return ans ``` ```java - +class Solution { + public int longestCommonPrefix(int[] arr1, int[] arr2) { + Set s = new HashSet<>(); + for (int x : arr1) { + for (; x > 0; x /= 10) { + s.add(x); + } + } + int ans = 0; + for (int x : arr2) { + for (; x > 0; x /= 10) { + if (s.contains(x)) { + ans = Math.max(ans, String.valueOf(x).length()); + break; + } + } + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int longestCommonPrefix(vector& arr1, vector& arr2) { + unordered_set s; + for (int x : arr1) { + for (; x; x /= 10) { + s.insert(x); + } + } + int ans = 0; + for (int x : arr2) { + for (; x; x /= 10) { + if (s.count(x)) { + ans = max(ans, (int) log10(x) + 1); + break; + } + } + } + return ans; + } +}; ``` ```go +func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) { + s := map[int]bool{} + for _, x := range arr1 { + for ; x > 0; x /= 10 { + s[x] = true + } + } + for _, x := range arr2 { + for ; x > 0; x /= 10 { + if s[x] { + ans = max(ans, int(math.Log10(float64(x)))+1) + break + } + } + } + return +} +``` +```ts +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); + } + } + let ans: number = 0; + for (let x of arr2) { + for (; x; x = (x / 10) | 0) { + if (s.has(x % 10)) { + ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); + } + } + } + return ans; +} ``` 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 e36ce785e2b80..aff873fab8b7b 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 @@ -46,24 +46,117 @@ Note that common prefixes between elements of the same array do not count. ## Solutions -### Solution 1 +### Solution 1: Hash Table + +We can use a hash table to store all the prefixes of the numbers in `arr1`. Then, we traverse all the numbers $x$ in `arr2`. For each number $x$, we start from the highest bit and gradually decrease, checking whether it exists in the hash table. If it does, we have found a common prefix, and we can update the answer accordingly. + +The time complexity is $O(m \times \log M + n \times \log N)$, and the space complexity is $O(m \times \log M)$. Here, $m$ and $n$ are the lengths of `arr1` and `arr2` respectively, and $M$ and $N$ are the maximum values in `arr1` and `arr2` respectively. ```python - +class Solution: + def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int: + s = set() + for x in arr1: + while x: + s.add(x) + x //= 10 + ans = 0 + for x in arr2: + while x: + if x in s: + ans = max(ans, len(str(x))) + break + x //= 10 + return ans ``` ```java - +class Solution { + public int longestCommonPrefix(int[] arr1, int[] arr2) { + Set s = new HashSet<>(); + for (int x : arr1) { + for (; x > 0; x /= 10) { + s.add(x); + } + } + int ans = 0; + for (int x : arr2) { + for (; x > 0; x /= 10) { + if (s.contains(x)) { + ans = Math.max(ans, String.valueOf(x).length()); + break; + } + } + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int longestCommonPrefix(vector& arr1, vector& arr2) { + unordered_set s; + for (int x : arr1) { + for (; x; x /= 10) { + s.insert(x); + } + } + int ans = 0; + for (int x : arr2) { + for (; x; x /= 10) { + if (s.count(x)) { + ans = max(ans, (int) log10(x) + 1); + break; + } + } + } + return ans; + } +}; ``` ```go +func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) { + s := map[int]bool{} + for _, x := range arr1 { + for ; x > 0; x /= 10 { + s[x] = true + } + } + for _, x := range arr2 { + for ; x > 0; x /= 10 { + if s[x] { + ans = max(ans, int(math.Log10(float64(x)))+1) + break + } + } + } + return +} +``` +```ts +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); + } + } + let ans: number = 0; + for (let x of arr2) { + for (; x; x = (x / 10) | 0) { + if (s.has(x % 10)) { + ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); + } + } + } + return ans; +} ``` diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.cpp b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.cpp new file mode 100644 index 0000000000000..1cde1f70ec00c --- /dev/null +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int longestCommonPrefix(vector& arr1, vector& arr2) { + unordered_set s; + for (int x : arr1) { + for (; x; x /= 10) { + s.insert(x); + } + } + int ans = 0; + for (int x : arr2) { + for (; x; x /= 10) { + if (s.count(x)) { + ans = max(ans, (int) log10(x) + 1); + break; + } + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.go b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.go new file mode 100644 index 0000000000000..fedf8c00c152e --- /dev/null +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.go @@ -0,0 +1,17 @@ +func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) { + s := map[int]bool{} + for _, x := range arr1 { + for ; x > 0; x /= 10 { + s[x] = true + } + } + for _, x := range arr2 { + for ; x > 0; x /= 10 { + if s[x] { + ans = max(ans, int(math.Log10(float64(x)))+1) + break + } + } + } + return +} \ No newline at end of file diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.java b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.java new file mode 100644 index 0000000000000..68ffa8bd4fc4d --- /dev/null +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.java @@ -0,0 +1,20 @@ +class Solution { + public int longestCommonPrefix(int[] arr1, int[] arr2) { + Set s = new HashSet<>(); + for (int x : arr1) { + for (; x > 0; x /= 10) { + s.add(x); + } + } + int ans = 0; + for (int x : arr2) { + for (; x > 0; x /= 10) { + if (s.contains(x)) { + ans = Math.max(ans, String.valueOf(x).length()); + break; + } + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.py b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.py new file mode 100644 index 0000000000000..82c53e181189d --- /dev/null +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.py @@ -0,0 +1,15 @@ +class Solution: + def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int: + s = set() + for x in arr1: + while x: + s.add(x) + x //= 10 + ans = 0 + for x in arr2: + while x: + if x in s: + ans = max(ans, len(str(x))) + break + x //= 10 + return ans 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 new file mode 100644 index 0000000000000..88c02857fb7fe --- /dev/null +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts @@ -0,0 +1,17 @@ +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); + } + } + let ans: number = 0; + for (let x of arr2) { + for (; x; x = (x / 10) | 0) { + if (s.has(x % 10)) { + ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); + } + } + } + return ans; +}