Problem · Trie
Longest Common Prefix of Number Pairs
Learn this problemProblem statement
Choose one positive integer from firstArray and one from secondArray. Their longest common prefix is the longest sequence of decimal digits shared from the leftmost digit.
Return the greatest common-prefix length over all cross-array pairs, or 0 when no pair shares its first digit.
Function
atabricksLongestCommonPrefixOfNumberPairs(firstArray: int[], secondArray: int[]) → intExamples
Example 1
firstArray = [25, 288, 2655, 54546, 54, 555]secondArray = [2, 255, 266, 244, 26, 5, 54547]return = 454546 and 54547 share the four-digit prefix 5454.
Example 2
firstArray = [25, 288, 2655, 544, 54, 555]secondArray = [2, 255, 266, 244, 26, 5, 5444444]return = 3544 is a complete three-digit prefix of 5444444.
Example 3
firstArray = [817, 99]secondArray = [1999, 1909]return = 0No cross-array pair shares its first digit.
Constraints
1 ≤ firstArray.length ≤ 5 · 10^41 ≤ firstArray[i] ≤ 10^9