How to find the longest common prefix of all the words in a string with Python

4 Answers

0 votes
import os

def common_prefix_from_string(s):
    words = s.split()
    return os.path.commonprefix(words)


s = "low lower lowest lowly lowland"

prefix = common_prefix_from_string(s)

print(prefix) 




'''
run:

low

'''


 



answered Mar 11 by avibootz
edited Mar 11 by avibootz
0 votes
def common_prefix_from_string(s):
    words = s.split()
    
    prefix = words[0]

    for w in words[1:]:
        while not w.startswith(prefix):
            prefix = prefix[:-1]
            if not prefix:
                return ""

    return prefix
 

s = "low lower lowest lowly lowland"

print(common_prefix_from_string(s))

 
 
'''
run:
 
low
 
'''

 



answered Mar 11 by avibootz
edited Mar 11 by avibootz
0 votes
def common_prefix_from_string(s):
    words = s.split()
    
    prefix = ""
    
    for chars in zip(*words):
        if len(set(chars)) == 1:
            prefix += chars[0]
        else:
            break
    
    return prefix


s = "low lower lowest lowly lowland"

print(common_prefix_from_string(s))

 
 
'''
run:
 
low
 
'''

 



answered Mar 11 by avibootz
0 votes
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'
 
'''


 



answered Mar 11 by avibootz

Related questions

...