You are given two arrays with positive integers arr1 and arr2.
A prefix of a positive integer is an integer formed by one or more of its digits, starting from its leftmost digit. For example, 123 is a prefix of the integer 12345, while 234 is not.
A common prefix of two integers a and b is an integer c, such that c is a prefix of both a and b. For example, 5655359 and 56554 have a common prefix 565 while 1223 and 43456do not have a common prefix.
You need to find the length of the longest common prefix between all pairs of integers (x, y) such that x belongs to arr1 and y belongs to arr2.
Return the length of the longest common prefix among all pairs. If no common prefix exists among them, return0.
Input: arr1 =[1,10,100], arr2 =[1000]Output: 3Explanation: There are 3pairs(arr1[i], arr2[j]):- The longest common prefix of(1,1000)is1.- The longest common prefix of(10,1000)is10.- The longest common prefix of(100,1000)is100.The longest common prefix is100with a length of 3.
Example 2:
1
2
3
4
Input: arr1 =[1,2,3], arr2 =[4,4,4]Output: 0Explanation: There exists no common prefix for any pair(arr1[i], arr2[j]), hence we return0.Note that common prefixes between elements of the same array do not count.
⏰ Time complexity: O(m * n * min(log10(a), log10(b)), where m is the length of arr1, n is the length of arr2, and a and b are typical values in the arrays. This accounts for the nested loops and digit comparisons.
🧺 Space complexity: O(1), as no additional space is used apart from a few variables.
Method 2 - String Conversion and naive prefix comparison#
Here is the approach:
Nested Loop for Integer Pairs:
For each pair (x, y), where x is from arr1 and y is from arr2, compute the length of the common prefix.
Common Prefix Function:
Compare the digits of the two integers as strings to find the length of the common prefix.
Update Maximum Length:
Track the maximum prefix length encountered during the nested loops.
⏰ Time complexity: O(m * n * min(l1, l2)), where m is the length of arr1, n is the length of arr2, and l1 and l2 are the lengths of integers (converted to strings).
classSolution:
deflongestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int:
prefix_set = set()
for num in arr1:
while num !=0and num notin prefix_set:
prefix_set.add(num)
num //=10 max_length =0for num in arr2:
original_num = num
while num !=0and num notin prefix_set:
num //=10if num !=0:
max_length = max(
max_length, len(str(original_num)) - len(str(num))
)
return max_length
⏰ Time complexity: O(m log10(a) + n log10(b))), where m is the length of arr1, n is the length of arr2, and a and b are typical values in these arrays. This accounts for the nested loops and digit comparisons.
🧺 Space complexity: O(m * l1), where l1 is the length of the longest integer in arr1
Using a prefix tree (Trie) can indeed be an efficient way to solve this problem. A Trie is ideal for problems involving prefixes, as it allows for efficient insertion and search operations.
⏰ Time complexity: O(m * l1 + n * l2), where m and n are the lengths of arr1 and arr2, and l1 and l2 are the number of digits in the longest numbers in arr1 and arr2.
🧺 Space complexity: O(m * l1), where m is the length of arr1 and l1 is the length of the longest number in arr1.