+ 1
i have probleme with code coach : longest common string
from difflib import SequenceMatcher names = input().split() string2 = names[0] for i in range(1, len(names)): string1 = string2 string2 = names[i] match = SequenceMatcher(None, string1, string2).find_longest_match(0, len(string1), 0, len(string2)) print(string1[match.a: match.a + match.size])
3 Respuestas
+ 1
# Hi, saad zerrai !
# You can compare your code to this one:
l = input().split()
print(max([min(l)[i:j] for i in range(len(min(l))) for j in range(i, len(min(l)) + 1) if all([min(l)[i:j] in wd for wd in l])], key=len))
+ 1
# Alexey Kopyshev
# Here's a version that satisfies your suggestion. (Both versions solves
# the code coach problem Longest Common Substring.)
words, substrings = set((input() or "ggaabb bbggaa aabbgg").split()), set()
shortest_word = min(words, key=len)
substrings = {shortest_word[i:j] for i in range(len(shortest_word)) for j in range(i + 1, len(shortest_word) + 1) if all(shortest_word[i:j] in word for word in words)}
print(next(substring for substring in sorted(substrings) if len(substring) == len(max(substrings, key=len))))
"""
Link to code:
https://sololearn.com/compiler-playground/cLnWK6YP2GRK/?ref=app
"""
0
Per Bratthammar For the solution to satisfy the condition "If there are multiple longest common substrings, output the smallest one in alphabetical order." you need to add something, like sorted before max.