Find Longest Common Prefix In List Of Strings | Python Example

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

I've done it like this (less pythonic surely):
for i in range(len(min_str)):
min_prefix = min_str[:i+1]
count = 0
for word in words:
if word[:i+1] == min_prefix:
count += 1
else:
break
if count == len(words):
max_prefix = min_prefix
or
for i in range(len(min_str)):
min_prefix = min_str[:i+1]
if len([min_prefix for word in words if word[:i+1] == min_prefix]) == len(words): but less prettier than the startwith method
else:
break
max_prefix = min_prefix

python