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 longest common prefix between all pairs of integers (x, y)
such that x
belongs to arr1
and y
belongs to arr2
.
Return the length of the longest common prefix among all pairs. If no common prefix exists among them, return 0
.
Example 1:
Input: arr1 = [1,10,100], arr2 = [1000] Output: 3 Explanation: There are 3 pairs (arr1[i], arr2[j]): - The longest common prefix of (1, 1000) is 1. - The longest common prefix of (10, 1000) is 10. - The longest common prefix of (100, 1000) is 100. The longest common prefix is 100 with a length of 3.
Example 2:
Input: arr1 = [1,2,3], arr2 = [4,4,4] Output: 0 Explanation: There exists no common prefix for any pair (arr1[i], arr2[j]), hence we return 0. Note that common prefixes between elements of the same array do not count.
Constraints:
1 <= arr1.length, arr2.length <= 5 * 104
1 <= arr1[i], arr2[i] <= 108
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find the longest set of characters that appear at the beginning of all the words in a given list. The brute force method involves checking progressively longer prefixes until we find one that doesn't match all words. If nothing matches, then the common prefix is nothing or 'empty'.
Here's how the algorithm would work step-by-step:
def find_longest_common_prefix_brute_force(list_of_strings):
if not list_of_strings:
return ""
longest_common_prefix = list_of_strings[0]
# Iterate while the prefix is not empty
while longest_common_prefix:
# Check if current prefix is a prefix of all strings
is_common = True
for current_string in list_of_strings[1:]:
if not current_string.startswith(longest_common_prefix):
is_common = False
break
# If it is a common prefix, return it
if is_common:
return longest_common_prefix
# Shorten the prefix by one character
longest_common_prefix = longest_common_prefix[:-1]
# If the loop finishes, no common prefix was found
return ""
To efficiently find the longest common prefix of a list of words, we'll compare the words character by character. The goal is to stop as soon as we find a character that doesn't match across all words.
Here's how the algorithm would work step-by-step:
def find_length_of_longest_common_prefix(list_of_strings):
if not list_of_strings:
return 0
prefix_length = 0
# Iterate char by char thru the first string.
for character_index in range(len(list_of_strings[0])):
character = list_of_strings[0][character_index]
# Compare current char against all other strings.
for string_index in range(1, len(list_of_strings)):
# Check if index is out of range for the current string.
if character_index >= len(list_of_strings[string_index]) or \
list_of_strings[string_index][character_index] != character:
return prefix_length
# All chars matched for all strings.
prefix_length += 1
return prefix_length
Case | How to Handle |
---|---|
Empty input array | Return 0 immediately as there are no strings to compare. |
Null input array | Throw an IllegalArgumentException or return 0 after a null check, indicating invalid input. |
Array with a single string | Return the length of the single string, as it is trivially the longest common prefix. |
All strings are identical | Return the length of any of the strings, as they all have the same length and are the common prefix. |
No common prefix exists (first characters are all different) | The algorithm should correctly identify this and return 0. |
One string is a prefix of another string (e.g., 'flower' and 'flow') | The algorithm should correctly identify 'flow' as the longest common prefix and return its length. |
Very long strings (performance consideration) | The algorithm's efficiency should be considered to avoid excessive iteration in very large strings; iterative solutions are generally preferred. |
Strings containing non-ASCII characters | The algorithm should work correctly with Unicode/UTF-8 encoded characters, ensuring correct character comparisons. |