Problem · Hash Table
Longest Common Prefix of Number Pairs
Learn this problemProblem statement
You are given two arrays of positive integers, arr1 and arr2.
The common prefix of two integers is the longest sequence of decimal digits they share starting from the leftmost digit. For example, 54546 and 54547 have the common prefix 5454, whose length is 4.
Choose one integer from arr1 and one integer from arr2. Return the maximum common-prefix length over all such cross-array pairs. Return 0 if no pair shares its first digit.
Function
longestCommonPrefixLength(arr1: int[], arr2: int[]) → intExamples
Example 1
arr1 = [25,288,2655,54546,54,555]arr2 = [2,255,266,244,26,5,54547]return = 454546 and 54547 share the four-digit prefix 5454. No cross-array pair shares a longer prefix.
Example 2
arr1 = [25,288,2655,544,54,555]arr2 = [2,255,266,244,26,5,5444444]return = 3The complete value 544 is a three-digit prefix of 5444444.
Example 3
arr1 = [817,99]arr2 = [1999,1909]return = 0No cross-array pair shares its first digit.
Constraints
- The length of each input array is between
1and50000, inclusive. - Every array value is between
1and1000000000, inclusive.