Algorithm
Find the Length of the Longest Common Prefix
Solution
class Solution:
def longestCommonPrefix(self, arr1, arr2):
arr1_prefixes = set()
# Step 1: Build all possible prefixes from arr1
for val in arr1:
while val not in arr1_prefixes and val > 0:
arr1_prefixes.add(val)
val //= 10
longest_prefix = 0
# Step 2: Check each number in arr2
for val in arr2:
while val not in arr1_prefixes and val > 0:
val //= 10
if val > 0:
longest_prefix = max(longest_prefix, len(str(val)))
return longest_prefixVideo GuideLeetcode Daily
Time Complexity
O(m d + n d)
Space Complexity
O(m d)
