Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Longest Common Prefix by catloverss
def longest_prefix(arr: list[str]) -> str:
if not arr:
return ""
# Take the shortest string as the maximum possible prefix
shortest = min(arr, key=len)
for i, char in enumerate(shortest):
for other in arr:
if other[i] != char:
return shortest[:i] # Return up to the mismatch
return shortest # All characters matched
Dec. 5, 2025
Comments: