Longest Shared Leading-Digit Sequence Between Two Integer Lists
StringsAuto-graded · 23 test casesLast updated
Tests whether a candidate can avoid brute-force pair comparison by exploiting bounded digit counts to match prefixes efficiently with a hash set.
Longest Shared Leading-Digit Sequence Between Two Integer Lists
You are given two lists of positive integers, nums1 and nums2. Your task is to find the length of the longest leading-digit sequence that appears as a prefix in at least one number from nums1 and also as a prefix in at least one number from nums2.
A decimal prefix of a positive integer is any string of its leading digits in base 10. For example, the integer 4729 has the prefixes "4", "47", "472", and "4729".
If no such shared prefix exists across any pairing, return 0.
Input and Output
- Input: Two lists of positive integers,
nums1andnums2. - Output: A single integer — the maximum length of any digit string that is a prefix of some element in
nums1and simultaneously a prefix of some element innums2.
Constraints
1 <= len(nums1), len(nums2) <= 5 * 10^41 <= nums1[i], nums2[i] <= 10^8- Elements within a list are not necessarily distinct.
Examples
Example 1
Input: max_shared_digit_prefix_length([1, 10, 100], [1000])
Output: 3
Example 2
Input: max_shared_digit_prefix_length([1234, 5678], [567, 999])
Output: 3
Example 3
Input: max_shared_digit_prefix_length([12, 34], [56, 78])
Output: 0
Hints
Hint 1
A brute-force approach checks every pair (x, y) — up to 2.5 billion comparisons at the maximum sizes. Think about what you can precompute from one list so that processing the other list becomes fast lookups instead of pairwise comparisons.
Hint 2
Each number is at most 10^8, which means at most 9 digits, so it has at most 9 prefixes. Store every prefix of every number in nums1 in a hash set, then probe each prefix of each number in nums2 against that set.
Hint 3
Once you have the prefix set built from nums1, iterate through nums2 and for each number check each of its prefixes from shortest to longest (or longest to shortest). The answer is the maximum length prefix that appears in both sets.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Why brute force is too slow
Checking every pair (x, y) with x ∈ nums1 and y ∈ nums2 takes O(n · m) time. At the maximum sizes that is up to 2.5 billion pair comparisons — far too slow.
Key observation: numbers are short
Every value is at most 10^8, which has 9 digits. So each number has at most 9 prefixes. Instead of matching numbers against numbers, we can match prefixes against prefixes:
- For each number in
nums1, generate all of its decimal prefixes ("4","47","472", …) and insert them into a hash set. - For each number in
nums2, generate its decimal prefixes and check each one against the set. Whenever a prefix of lengthiis found in the set, update the running maximum.
This converts a pairwise comparison problem into a series of O(1) hash-set lookups.
Why it is correct
Soundness: If prefix p of some y ∈ nums2 (length i) appears in the set, it was inserted as a prefix of some x ∈ nums1. So x and y genuinely share a leading-digit sequence of length i, and the answer is at least i.
Completeness: Suppose the true best is achieved by a pair (x*, y*) sharing a prefix p* of length L. Then p* was inserted when processing x*, and it gets probed when processing y*. So best reaches at least L.
Zero case: If no pair shares even a single leading digit, no probe ever matches and the function correctly returns 0.
Complexity
Let n = len(nums1), m = len(nums2), and D ≤ 9 be the maximum digit length.
- Time: O((n + m) · D²) in the literal string-slicing form, but since D is a small constant (≤ 9) this is effectively O(n + m).
- Space: O(n · D) for the prefix set — at most around 450 000 strings, each of length ≤ 9.
Edge cases
- Single-digit numbers: They have exactly one prefix, so any match involving them contributes at most 1. Handled naturally.
- No common prefix: The set is never probed successfully; returns
0. - Duplicates: The set deduplicates entries from
nums1. Duplicates innums2repeat lookups harmlessly. - One number is a prefix of another (e.g.
100and1000): The full digit string of the shorter number is a prefix of both, so the answer equals the shorter number's length. The algorithm handles this because it generates all prefixes up to and including the full digit string. - Identical numbers across lists: Their full digit string matches, giving the maximum possible answer for those values.
Reference solution
from typing import List
def max_shared_digit_prefix_length(nums1: List[int], nums2: List[int]) -> int:
# Build a set of every decimal prefix of every number in nums1.
prefix_set = set()
for num in nums1:
s = str(num)
for i in range(1, len(s) + 1):
prefix_set.add(s[:i])
# Probe each prefix of each number in nums2 against the set.
best = 0
for num in nums2:
s = str(num)
for i in range(1, len(s) + 1):
if s[:i] in prefix_set:
best = max(best, i)
return best