Class Solution
-
- All Implemented Interfaces:
public final class Solution3043 - Find the Length of the Longest Common Prefix.
Medium
You are given two arrays with positive integers
arr1andarr2.A prefix of a positive integer is an integer formed by one or more of its digits, starting from its leftmost digit. For example,
123is a prefix of the integer12345, while234is not.A common prefix of two integers
aandbis an integerc, such thatcis a prefix of bothaandb. For example,5655359and56554have a common prefix565while1223and43456do not have a common prefix.You need to find the length of the longest common prefix between all pairs of integers
(x, y)such thatxbelongs toarr1andybelongs toarr2.Return the length of the longest common prefix among all pairs. If no common prefix exists among them, return
0.Example 1:
Input: arr1 = 1,10,100, arr2 = 1000
Output: 3
Explanation: There are 3 pairs (arr1i, arr2j):
The longest common prefix of (1, 1000) is 1.
The longest common prefix of (10, 1000) is 10.
The longest common prefix of (100, 1000) is 100.
The longest common prefix is 100 with a length of 3.
Example 2:
Input: arr1 = 1,2,3, arr2 = 4,4,4
Output: 0
Explanation: There exists no common prefix for any pair (arr1i, arr2j), hence we return 0.
Note that common prefixes between elements of the same array do not count.
Constraints:
<code>1 <= arr1.length, arr2.length <= 5 * 10<sup>4</sup></code>
<code>1 <= arr1i, arr2i<= 10<sup>8</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerlongestCommonPrefix(IntArray arr1, IntArray arr2)-
-
Method Detail
-
longestCommonPrefix
final Integer longestCommonPrefix(IntArray arr1, IntArray arr2)
-
-
-
-