AlgoMaster Logo

Find the Length of the Longest Common Prefix

mediumFrequencyUpdated September 21, 2026

Understanding the Problem

We have two arrays of positive integers. For every possible pair where one number comes from arr1 and the other from arr2, we need to find how many leading digits they share, and return the maximum such count across all pairs.

For example, if arr1 has the number 12345 and arr2 has 12367, their common prefix is "123", so they share 3 leading digits. We want the pair across the two arrays that shares the longest such leading sequence.

We don't need to check every pair individually. The prefixes of a number form a small set (at most 9 of them). If we store every prefix that appears in one array, we can look up whether the other array's numbers reuse any of those prefixes. This turns "compare all pairs" into "which prefixes do the two arrays have in common?"

Key Constraints:

  • 1 <= arr1.length, arr2.length <= 5 * 10^4 → With up to 50,000 elements in each array, a brute force approach that checks all pairs would do up to 2.5 billion pair comparisons. That's too slow. We need something better than O(m * n).
  • 1 <= arr1[i], arr2[i] <= 10^8 → Each number has at most 9 digits. This means the "prefix work" per number is bounded by a small constant (at most 9 prefixes per number). Any approach that does O(d) work per number, where d is the number of digits, is effectively linear in the array size.

Approach 1: Brute Force

Intuition

For every pair (x, y) where x comes from arr1 and y comes from arr2, convert both numbers to strings and compare them character by character from the left. Count how many leading characters match, and track the maximum across all pairs.

This mirrors comparing two numbers by hand: line them up at the leftmost digit and count how many positions agree before the first mismatch.

Algorithm

  1. Initialize maxLength = 0.
  2. For each number x in arr1:
    • For each number y in arr2:
      • Convert both to strings.
      • Compare characters from the start, counting how many match consecutively.
      • Update maxLength with the count if it's larger.
  3. Return maxLength.

Visualization and Code

Loading animation...

Comparing every pair is too slow for the given constraints. The next approach precomputes all the prefixes from one array once, then reuses them for every lookup.

Approach 2: Hash Set of Prefixes

Intuition

Instead of comparing pairs of numbers, we can split the problem into two phases. First, collect every prefix from all numbers in arr1. Then, for each number in arr2, generate its prefixes and check which ones already appear in that collection.

To generate all prefixes of a number, repeatedly divide by 10. Take 12345: dividing gives 12345, 1234, 123, 12, 1, where each division strips the last digit to produce the next shorter prefix. A prefix represented as an integer is unique, so two numbers share leading digits exactly when they produce the same prefix value.

Store all prefixes from arr1 in a hash set. Then for each number in arr2, generate its prefixes from longest to shortest and check each against the set. The longest match found across all arr2 numbers is the answer.

Algorithm

  1. Create an empty hash set prefixSet.
  2. For each number x in arr1:
    • While x > 0, add x to prefixSet, then set x = x / 10.
  3. Initialize maxLength = 0.
  4. For each number y in arr2:
    • While y > 0:
      • If y is in prefixSet, update maxLength with the number of digits in y, then break (longest prefix for this number found).
      • Set y = y / 10.
  5. Return maxLength.

Visualization and Code

Loading animation...

The hash set stores each prefix as a separate entry, even when many numbers share the same leading digits. The next approach uses a trie, which stores shared prefixes once as a common path.

Approach 3: Trie

Intuition

A trie (prefix tree) stores prefixes as paths in a tree. Each edge represents one digit, and each path from the root to a node spells out a prefix. Building the trie from arr1 makes every prefix of every arr1 number a path, with shared leading digits collapsed onto a single shared path.

Take the number 12345. We insert the digits 1, 2, 3, 4, 5 as a path. To compare 12367 against it, we walk the trie following 1, 2, 3, then stop at digit 6 because that node has no child for 6 (it has a child for 4). The depth reached, 3, is the longest common prefix between 12367 and 12345.

Sharing is what makes this compact. If arr1 has 123, 1234, and 456, the trie stores the path 1 to 2 to 3 to 4 once rather than once per number, plus a separate branch 4 to 5 to 6.

Algorithm

  1. Build a trie from all numbers in arr1:
    • For each number, extract its digits from left to right.
    • Insert each digit as a child node, creating new nodes as needed.
  2. For each number in arr2, traverse the trie:
    • Extract digits from left to right.
    • Follow the trie path as long as matching children exist.
    • The depth reached is the common prefix length for this number.
  3. Track and return the maximum depth across all arr2 numbers.

Visualization and Code

Loading animation...