How to find the longest shared prefix among all words in a string with Python

1 Answer

0 votes
import re
from collections import defaultdict
 
def group_by_all_prefixes(s):
    words = re.findall(r"[a-zA-Z]+", s.lower())
    groups = defaultdict(list)
 
    for w in words:
        for i in range(1, len(w) + 1):
            groups[w[:i]].append(w)
 
    return dict(groups)
 
 
s = "The Lowly inhabitants of the lowland were surprised to see the lower branches of the trees."
 
groups = group_by_all_prefixes(s)
 
# Show only prefixes that appear in 2+ words
filtered = {p: ws for p, ws in groups.items() if len(ws) >= 2}
 
# Find the longest prefix
longest_prefix = max(filtered, key=len)
 
print("Longest shared prefix:")
print(f"{longest_prefix} | prefix_len={len(longest_prefix)} | group_count={len(filtered[longest_prefix])} | {filtered[longest_prefix]}")



'''
run:

Longest shared prefix:
lowl | prefix_len=4 | group_count=2 | ['lowly', 'lowland']

'''

 



answered Mar 12 by avibootz

Related questions

...