Skip to content

Commit a2ffdb4

Browse files
committed
[LeetCode Sync] Runtime - 206 ms (79.27%), Memory - 28.8 MB (78.62%)
1 parent 6e53faa commit a2ffdb4

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<p>You are given two arrays with <strong>positive</strong> integers <code>arr1</code> and <code>arr2</code>.</p>
2+
3+
<p>A <strong>prefix</strong> of a positive integer is an integer formed by one or more of its digits, starting from its <strong>leftmost</strong> digit. For example, <code>123</code> is a prefix of the integer <code>12345</code>, while <code>234</code> is <strong>not</strong>.</p>
4+
5+
<p>A <strong>common prefix</strong> of two integers <code>a</code> and <code>b</code> is an integer <code>c</code>, such that <code>c</code> is a prefix of both <code>a</code> and <code>b</code>. For example, <code>5655359</code> and <code>56554</code> have common prefixes <code>565</code> and <code>5655</code> while <code>1223</code> and <code>43456</code> <strong>do not</strong> have a common prefix.</p>
6+
7+
<p>You need to find the length of the <strong>longest common prefix</strong> between all pairs of integers <code>(x, y)</code> such that <code>x</code> belongs to <code>arr1</code> and <code>y</code> belongs to <code>arr2</code>.</p>
8+
9+
<p>Return <em>the length of the <strong>longest</strong> common prefix among all pairs</em>.<em> If no common prefix exists among them</em>, <em>return</em> <code>0</code>.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> arr1 = [1,10,100], arr2 = [1000]
16+
<strong>Output:</strong> 3
17+
<strong>Explanation:</strong> There are 3 pairs (arr1[i], arr2[j]):
18+
- The longest common prefix of (1, 1000) is 1.
19+
- The longest common prefix of (10, 1000) is 10.
20+
- The longest common prefix of (100, 1000) is 100.
21+
The longest common prefix is 100 with a length of 3.
22+
</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> arr1 = [1,2,3], arr2 = [4,4,4]
28+
<strong>Output:</strong> 0
29+
<strong>Explanation:</strong> There exists no common prefix for any pair (arr1[i], arr2[j]), hence we return 0.
30+
Note that common prefixes between elements of the same array do not count.
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>1 &lt;= arr1.length, arr2.length &lt;= 5 * 10<sup>4</sup></code></li>
38+
<li><code>1 &lt;= arr1[i], arr2[i] &lt;= 10<sup>8</sup></code></li>
39+
</ul>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int:
3+
set_vals = set()
4+
for val in arr1:
5+
while val:
6+
set_vals.add(val)
7+
val //= 10
8+
9+
result = 0
10+
for val in arr2:
11+
while val:
12+
if val in set_vals:
13+
result = max(result, len(str(val)))
14+
break
15+
val //= 10
16+
17+
return result

0 commit comments

Comments
 (0)