import re
import os
def longest_common_prefix(s):
# Extract words and handle empty input
words = re.findall(r"[a-z]+", s.lower())
if not words:
return ""
# Option A: LCP for ALL words in the string
# Using os.path.commonprefix is the most "Pythonic" way
return os.path.commonprefix(words)
def longest_common_prefix_efficient(s):
words = re.findall(r"[a-z]+", s.lower())
if not words: return ""
# Option B: LCP for ALL words (Sorting method)
# The LCP of a sorted list is just the LCP of the first and last elements
words.sort()
first, last = words[0], words[-1]
i = 0
while i < len(first) and i < len(last) and first[i] == last[i]:
i += 1
return first[:i]
s1 = "The lowly inhabitants of the lowland were surprised to see the lower branches of the trees."
# Result is "" because "the" and "lowly" share no prefix.
print(f"LCP: '{longest_common_prefix_efficient(s1)}'")
s2 = "unclear, uncertain, unexpected"
# Result is "un"
print(f"LCP: '{longest_common_prefix_efficient(s2)}'")
'''
run:
LCP: ''
LCP: 'un'
'''