Advertisement

Find the Length of the Longest Common Prefix - LeetCode 3043 Solution

Find the Length of the Longest Common Prefix - Complete Solution Guide

Find the Length of the Longest Common Prefix is LeetCode problem 3043, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given two arrays with positive integers arr1 and arr2 . A prefix of a positive integer is an integer formed by one or more of its digits, starting from its leftmost digit. For example, 123 is a prefix of the integer 12345 , while 234 is not . A common prefix of two integers a and b is an integer c , such that c is a prefix of both a and b . For example, 5655359 and 56554 have common prefixes 565 and 5655 while 1223 and 43456 do not have a common prefix. You need to find the length of the

Detailed Explanation

The problem asks us to find the length of the longest common prefix between pairs of numbers, where one number comes from the array `arr1` and the other from `arr2`. A prefix of a number is a substring that starts from the beginning of the number (e.g., '12' is a prefix of '1234', but '23' is not). The goal is to iterate through all possible pairs of numbers (one from `arr1` and one from `arr2`), find the longest common prefix for each pair, and then return the length of the *longest* of all these common prefixes. If no pair shares any prefix, the function should return 0. The input arrays contain positive integers, and the prefix must be composed of digits starting from the leftmost digit of each integer.

Solution Approach

The provided solution uses a Trie data structure to efficiently find the longest common prefix. First, the integer arrays `arr1` and `arr2` are converted into sets of strings. A Trie is then constructed using strings from the smaller set. After the Trie is built, each string from the larger set is searched within the Trie to find the longest matching prefix. The maximum length encountered during these searches is returned as the result.

Step-by-Step Algorithm

  1. Step 1: Convert the integer arrays `arr1` and `arr2` into sets of strings (`build_set` and `search_set`). Using sets removes any duplicate entries.
  2. Step 2: Check which set is smaller (`build_set` or `search_set`) and assign it to `build_set`. The smaller set is used to build the Trie for optimization as it generally results in smaller Trie structures.
  3. Step 3: Create a Trie data structure. Each node in the Trie represents a digit, and paths from the root to other nodes represent prefixes.
  4. Step 4: Insert all strings from `build_set` into the Trie. Each digit of each string is added as a child node in the Trie.
  5. Step 5: Iterate through each string in `search_set` and search for the longest matching prefix within the Trie.
  6. Step 6: During the Trie search, keep track of the length of the current matching prefix. If a character in the `search_set` string is not found in the Trie, the search for that string stops.
  7. Step 7: Update the `max_length` with the maximum prefix length found so far.
  8. Step 8: Return the `max_length`, which represents the length of the longest common prefix among all pairs.

Key Insights

  • Insight 1: Converting integers to strings simplifies the prefix comparison because string comparison by character is a straightforward operation.
  • Insight 2: Using a Trie data structure can efficiently store prefixes of numbers from one array, allowing for fast prefix searching when comparing with numbers from the other array.
  • Insight 3: Converting the input arrays to sets prevents duplicate calculations and allows to build the Trie with unique prefixes only

Complexity Analysis

Time Complexity: O(n*m)

Space Complexity: O(n*m)

Topics

This problem involves: Array, Hash Table, String, Trie.

Companies

Asked at: Capital One, Coinbase, Databricks, Roblox, The Trade Desk, Visa, ZipRecruiter.