i have probleme with code coach : longest common string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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])

10th Sep 2023, 7:55 PM
saad zerrai
saad zerrai - avatar
3 Answers
+ 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))
10th Sep 2023, 8:54 PM
Per Bratthammar
Per Bratthammar - avatar
+ 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 """
12th Sep 2023, 9:21 PM
Per Bratthammar
Per Bratthammar - avatar
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.
10th Sep 2023, 9:31 PM
Alexey Kopyshev
Alexey Kopyshev - avatar