For this program I will have to strings and need to generate the longest common
ID: 3622167 • Letter: F
Question
For this program I will have to strings and need to generate the longest common substrings. The code for getting all subtrings of the first string is as follows:string1 = raw_input ("Enter first string: ")
size = len(string1)
for sub_len in range (size + 1, 1, -1):
for idx in range (0, size - sub_len + 1):
substr= string1[idx: idx + sub_len]
print substr
This returns all substrings starting with the longest substring, so how do i get the code to find if the substrings exist in the second string? (I know I can use the find() function I just don't know how)
Explanation / Answer
Dear, Giving you code in python to find longest substring in given string def long_substr(data): substr = '' if len(data) > 1 and len(data[0]) > 0: for i in range(len(data[0])): for j in range(len(data[0])-i+1): if j > len(substr) and all(data[0][i:i+j] in x for x in data): substr = data[0][i:i+j] return substr Hope this will help you
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.