So i started with the code from an answer to this question Function to find all common substrings in two strings not giving correct output and modified it a little bit to accommodate case-independence (i.e. AbCd is the same as ABCD as Abcd and so on) by turning the string to lowercase. However, for strings like 'ABCDXGHIJ'
and 'ghijYAbCd'
, it only returns ['ghij']
, not the desired output ['ABCD', 'GHIJ']
.
Here are other examples:
'Bonywasawarrior'
and'Bonywasxwarrior'
(output:['Bonywas', 'warrior', 'wa']
, desired output:['Bonywas', 'warrior']
)'01101001'
and'101010'
(output:['1010', '0', '1010', '01', '10', '01']
, desired output:['1010']
)
here is my code:
t = int(input()) #t cases
while t > 0:
A = str(input()) #1st string
B = str(input()) #2nd string
low_A = A.lower()
low_B = B.lower()
answer = ""
anslist=[]
for i in range(len(A)):
common = ""
for j in range(len(B)):
if (i + j < len(A) and low_A[i + j] == low_B[j]):
common += B[j]
else:
#if (len(common) > len(answer)):
answer = common
if answer != '' and len(answer) > 1:
anslist.append(answer)
common = ""
if common != '':
anslist.append(common)
if len(anslist) == 0:
print('[]') #print if no common substring
else:
print(anslist)
t -= 1