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 janesh.sivaruban2
def longest_prefix(arr: list[str]) -> str:
res = arr.pop()
l = len(res)
while arr:
sec, i = arr.pop(), 0
while i < min(l, len(sec)) and res[i] == sec[i]: i += 1
if not (l:= i): return ""
return res[:l]
print("Example:")
print(repr(longest_prefix(["flower", "flow", "flight"])))
# These "asserts" are used for self-checking
assert longest_prefix(["flower", "flow", "flight"]) == "fl"
assert longest_prefix(["dog", "racecar", "car"]) == ""
assert longest_prefix(["apple", "application", "appetizer"]) == "app"
assert longest_prefix(["a"]) == "a"
assert longest_prefix(["", "b"]) == ""
assert longest_prefix(["same", "same", "same"]) == "same"
assert longest_prefix(["mismatch", "mister", "miss"]) == "mis"
assert longest_prefix(["alphabet", "alpha", "alphadog"]) == "alpha"
assert longest_prefix(["book", "boot", "booster"]) == "boo"
assert longest_prefix(["short", "shore", "shot"]) == "sho"
print("The mission is done! Click 'Check Solution' to earn rewards!")
Oct. 17, 2025
Comments: