+ 1

# Longest Common String (Test Case #4)

I have done my code,but Test Case #4 failed.I can't find the bug, could you hepl me ? My code url: https://code.sololearn.com/cjl34y2071rd Task: Given multiple words, you need to find the longest string that is a substring of all words. Input Format: A string of words, separated by spaces. The string can also contain numbers. Output Format: A string, representing the longest common substring. If there are multiple longest common substrings, output the smallest one in alphabetical order. Sample Input: SoloLearn Learning LearningIsFun Learnable Sample Output: Learn

24th Dec 2021, 7:55 AM
wackyd
6 ответов
+ 2
1. Split string 2. Sort array 3. Sort array by length of the items in reverse order 4. Take the first one
24th Dec 2021, 7:43 PM
Alexey Kopyshev
0
Thank you for your help, it works when I used your suggestion. And I really want to know,why sort by length can pass the Test Case #4.Looking forward to your reply.
26th Dec 2021, 3:26 PM
wackyd
0
My solution od this task: import re from itertools import combinations string = input().split(" ") def common_string(string): string_list = combinations(string, 2) substring = [] for strings in string_list: y = 0 z = 1 while z <= len(strings[0]) - 1: if re.search(strings[0][y : z], strings[1]): string = strings[0][y : z] if z == len(strings[0]) - 1: if re.search(strings[0][y :], strings[1]): string = strings[0][y :] z += 1 else: y = z - 1 z += 1 substring.append(string) if len(set(substring)) == 1: print("".join(set(substring))) else: common_string(set(substring)) common_string(string)
25th Apr 2022, 9:18 PM
Przemysław Komański
0
Hi guys, I have a similar issue. # Check if the length of the result is greater than one or print the result if len(commons) > 1: commons = list(sorted(commons, key=str.lower)) commons = list(sorted(commons, key=len, reverse=True)) lens = [len(x) for x in commons] maxlen = max(lens) maxcount = lens.count(maxlen) if maxcount > 1: maxcoms = [x for x in commons if len(x) == maxlen] maxcoms = list(sorted(maxcoms, key=str.lower)) print(maxcoms[0]) else: print(commons[0]) else: print(commons[0])
27th Mar 2023, 1:59 PM
William Mabotja
0
Failing test case 4
27th Mar 2023, 1:59 PM
William Mabotja
0
# Grab input and split to list str_input = input().split(" ") def mutate(x): """ Creates all possible permutations for a given string. """ start = 0 while start < len(x): i = 0 end = 1 while i < len(x): yield x[start:end] end += 1 i += 1 start += 1 # List to hold all possible permutations mutations = [] # Run generator function on all list elememts and append to mutations list for i in str_input: for j in mutate(i): mutations.append(j) # List to hold common permutations commons = [] # Find words that occur in each list item for i in mutations: common = set([x for x in str_input if(i in x)]) if len(common) == len(str_input): commons.append(i) # Check if the length of the result is greater than one or print the result if len(commons) > 1: commons = list(sorted(commons, key=str.lower)) commons = list(sorted(commons, key=len, reverse=True))
27th Mar 2023, 2:00 PM
William Mabotja
Актуальное сегодня