Ctrl K

Shared Leading Digits

Medium
You are given two lists of positive integers, left and right. A leading part of a number is the number formed by one or more of its first digits, read from the left. For example, 3051 has leading parts 3, 30, 305 and 3051, while 51 is not one of them. Pick one number from left and one from right and find the longest leading part they share. Return the greatest length (in digits) of such a shared leading part over all possible pairs, or 0 if no pair shares one. Pairs are only formed across the two lists; numbers in the same list are never compared.
Example 1
left=[47, 4801, 9], right=[480, 470] → 3

4801 and 480 both start with 480, which is three digits. That beats 47 and 470 (two digits).

Example 2
left=[12, 34], right=[56, 21] → 0

No pair starts with the same digit, so the answer is 0. Numbers within the same list are never compared to each other.

Example 3
left=[7], right=[7] → 1
Constraints
  • 1 ≤ len(left), len(right) ≤ 5·10⁴
  • 1 ≤ left[i], right[i] ≤ 10¹⁰
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSplit Into Dictionary Words MediumHighlight Search Hits MediumShorten Words to Their Roots MediumMost Searched Words MediumWord Grown Letter by Letter Medium
loading Python…
Input
left=[47, 4801, 9], right=[480, 470]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 118 tests on the server, hidden ones included.