Skip to content

Commit ba69ef5

Browse files
authored
feat: add solutions to lc problem: No.3043 (doocs#2351)
No.3043.Find the Length of the Longest Common Prefix
1 parent 1048eef commit ba69ef5

File tree

7 files changed

+284
-8
lines changed

7 files changed

+284
-8
lines changed

solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md

+97-4
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,117 @@
5050

5151
## 解法
5252

53-
### 方法一
53+
### 方法一:哈希表
54+
55+
我们可以使用哈希表来存储 `arr1` 中的所有数字的前缀,然后遍历 `arr2` 中的所有数字 $x$,对于每个数字 $x$,我们从最高位开始逐渐减小,判断是否存在于哈希表中,如果存在,那么我们就找到了一个公共前缀,此时更新答案即可。
56+
57+
时间复杂度 $O(m \times \log M + n \times \log N)$,空间复杂度 $O(m \times \log M)$。其中 $m$ 和 $n$ 分别是 `arr1``arr2` 的长度,而 $M$ 和 $N$ 分别是 `arr1``arr2` 中的最大值。
5458

5559
<!-- tabs:start -->
5660

5761
```python
58-
62+
class Solution:
63+
def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int:
64+
s = set()
65+
for x in arr1:
66+
while x:
67+
s.add(x)
68+
x //= 10
69+
ans = 0
70+
for x in arr2:
71+
while x:
72+
if x in s:
73+
ans = max(ans, len(str(x)))
74+
break
75+
x //= 10
76+
return ans
5977
```
6078

6179
```java
62-
80+
class Solution {
81+
public int longestCommonPrefix(int[] arr1, int[] arr2) {
82+
Set<Integer> s = new HashSet<>();
83+
for (int x : arr1) {
84+
for (; x > 0; x /= 10) {
85+
s.add(x);
86+
}
87+
}
88+
int ans = 0;
89+
for (int x : arr2) {
90+
for (; x > 0; x /= 10) {
91+
if (s.contains(x)) {
92+
ans = Math.max(ans, String.valueOf(x).length());
93+
break;
94+
}
95+
}
96+
}
97+
return ans;
98+
}
99+
}
63100
```
64101

65102
```cpp
66-
103+
class Solution {
104+
public:
105+
int longestCommonPrefix(vector<int>& arr1, vector<int>& arr2) {
106+
unordered_set<int> s;
107+
for (int x : arr1) {
108+
for (; x; x /= 10) {
109+
s.insert(x);
110+
}
111+
}
112+
int ans = 0;
113+
for (int x : arr2) {
114+
for (; x; x /= 10) {
115+
if (s.count(x)) {
116+
ans = max(ans, (int) log10(x) + 1);
117+
break;
118+
}
119+
}
120+
}
121+
return ans;
122+
}
123+
};
67124
```
68125
69126
```go
127+
func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) {
128+
s := map[int]bool{}
129+
for _, x := range arr1 {
130+
for ; x > 0; x /= 10 {
131+
s[x] = true
132+
}
133+
}
134+
for _, x := range arr2 {
135+
for ; x > 0; x /= 10 {
136+
if s[x] {
137+
ans = max(ans, int(math.Log10(float64(x)))+1)
138+
break
139+
}
140+
}
141+
}
142+
return
143+
}
144+
```
70145

146+
```ts
147+
function longestCommonPrefix(arr1: number[], arr2: number[]): number {
148+
const s: Set<number> = new Set<number>();
149+
for (let x of arr1) {
150+
for (; x; x = (x / 10) | 0) {
151+
s.add(x % 10);
152+
}
153+
}
154+
let ans: number = 0;
155+
for (let x of arr2) {
156+
for (; x; x = (x / 10) | 0) {
157+
if (s.has(x % 10)) {
158+
ans = Math.max(ans, Math.floor(Math.log10(x)) + 1);
159+
}
160+
}
161+
}
162+
return ans;
163+
}
71164
```
72165

73166
<!-- tabs:end -->

solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md

+97-4
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,117 @@ Note that common prefixes between elements of the same array do not count.
4646

4747
## Solutions
4848

49-
### Solution 1
49+
### Solution 1: Hash Table
50+
51+
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.
52+
53+
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.
5054

5155
<!-- tabs:start -->
5256

5357
```python
54-
58+
class Solution:
59+
def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int:
60+
s = set()
61+
for x in arr1:
62+
while x:
63+
s.add(x)
64+
x //= 10
65+
ans = 0
66+
for x in arr2:
67+
while x:
68+
if x in s:
69+
ans = max(ans, len(str(x)))
70+
break
71+
x //= 10
72+
return ans
5573
```
5674

5775
```java
58-
76+
class Solution {
77+
public int longestCommonPrefix(int[] arr1, int[] arr2) {
78+
Set<Integer> s = new HashSet<>();
79+
for (int x : arr1) {
80+
for (; x > 0; x /= 10) {
81+
s.add(x);
82+
}
83+
}
84+
int ans = 0;
85+
for (int x : arr2) {
86+
for (; x > 0; x /= 10) {
87+
if (s.contains(x)) {
88+
ans = Math.max(ans, String.valueOf(x).length());
89+
break;
90+
}
91+
}
92+
}
93+
return ans;
94+
}
95+
}
5996
```
6097

6198
```cpp
62-
99+
class Solution {
100+
public:
101+
int longestCommonPrefix(vector<int>& arr1, vector<int>& arr2) {
102+
unordered_set<int> s;
103+
for (int x : arr1) {
104+
for (; x; x /= 10) {
105+
s.insert(x);
106+
}
107+
}
108+
int ans = 0;
109+
for (int x : arr2) {
110+
for (; x; x /= 10) {
111+
if (s.count(x)) {
112+
ans = max(ans, (int) log10(x) + 1);
113+
break;
114+
}
115+
}
116+
}
117+
return ans;
118+
}
119+
};
63120
```
64121
65122
```go
123+
func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) {
124+
s := map[int]bool{}
125+
for _, x := range arr1 {
126+
for ; x > 0; x /= 10 {
127+
s[x] = true
128+
}
129+
}
130+
for _, x := range arr2 {
131+
for ; x > 0; x /= 10 {
132+
if s[x] {
133+
ans = max(ans, int(math.Log10(float64(x)))+1)
134+
break
135+
}
136+
}
137+
}
138+
return
139+
}
140+
```
66141

142+
```ts
143+
function longestCommonPrefix(arr1: number[], arr2: number[]): number {
144+
const s: Set<number> = new Set<number>();
145+
for (let x of arr1) {
146+
for (; x; x = (x / 10) | 0) {
147+
s.add(x % 10);
148+
}
149+
}
150+
let ans: number = 0;
151+
for (let x of arr2) {
152+
for (; x; x = (x / 10) | 0) {
153+
if (s.has(x % 10)) {
154+
ans = Math.max(ans, Math.floor(Math.log10(x)) + 1);
155+
}
156+
}
157+
}
158+
return ans;
159+
}
67160
```
68161

69162
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int longestCommonPrefix(vector<int>& arr1, vector<int>& arr2) {
4+
unordered_set<int> s;
5+
for (int x : arr1) {
6+
for (; x; x /= 10) {
7+
s.insert(x);
8+
}
9+
}
10+
int ans = 0;
11+
for (int x : arr2) {
12+
for (; x; x /= 10) {
13+
if (s.count(x)) {
14+
ans = max(ans, (int) log10(x) + 1);
15+
break;
16+
}
17+
}
18+
}
19+
return ans;
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) {
2+
s := map[int]bool{}
3+
for _, x := range arr1 {
4+
for ; x > 0; x /= 10 {
5+
s[x] = true
6+
}
7+
}
8+
for _, x := range arr2 {
9+
for ; x > 0; x /= 10 {
10+
if s[x] {
11+
ans = max(ans, int(math.Log10(float64(x)))+1)
12+
break
13+
}
14+
}
15+
}
16+
return
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int longestCommonPrefix(int[] arr1, int[] arr2) {
3+
Set<Integer> s = new HashSet<>();
4+
for (int x : arr1) {
5+
for (; x > 0; x /= 10) {
6+
s.add(x);
7+
}
8+
}
9+
int ans = 0;
10+
for (int x : arr2) {
11+
for (; x > 0; x /= 10) {
12+
if (s.contains(x)) {
13+
ans = Math.max(ans, String.valueOf(x).length());
14+
break;
15+
}
16+
}
17+
}
18+
return ans;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int:
3+
s = set()
4+
for x in arr1:
5+
while x:
6+
s.add(x)
7+
x //= 10
8+
ans = 0
9+
for x in arr2:
10+
while x:
11+
if x in s:
12+
ans = max(ans, len(str(x)))
13+
break
14+
x //= 10
15+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function longestCommonPrefix(arr1: number[], arr2: number[]): number {
2+
const s: Set<number> = new Set<number>();
3+
for (let x of arr1) {
4+
for (; x; x = (x / 10) | 0) {
5+
s.add(x % 10);
6+
}
7+
}
8+
let ans: number = 0;
9+
for (let x of arr2) {
10+
for (; x; x = (x / 10) | 0) {
11+
if (s.has(x % 10)) {
12+
ans = Math.max(ans, Math.floor(Math.log10(x)) + 1);
13+
}
14+
}
15+
}
16+
return ans;
17+
}

0 commit comments

Comments
 (0)