This takes two strings and returns the length of the largest common substring.
Example:
contando_sstring_igual("abcdef", "cdofhij")
# 2
Because the longest common substring = "cd", so it returns the length of it (2).
Here is the code:
def contando_sstring_igual(string1, string2):
resposta = ""
tamanho1 = len(string1)
tamanho2 = len(string2)
for i in range(tamanho1):
for j in range(tamanho2):
lcs_temp = 0
igual = ''
while i + lcs_temp < tamanho1 and j + lcs_temp < tamanho2 and string1[i + lcs_temp] == string2[j + lcs_temp]:
igual += string2[j+lcs_temp]
lcs_temp += 1
if len(igual) > len(resposta):
resposta = igual
return len(resposta)
while True:
a = input()
b = input()
if a is None or b is None:
break
else:
print(contando_sstring_igual(a, b))
I need to make my code work significantly faster. It does the job right, but not fast enough. I need to avoid the timeout error I'm getting on the exercise and I'm not even close to figuring out how to do it, without the loop.