def substrings(a, b, n):
"""Return substrings of length n in both a and b"""
# takes in strings input a and b
# splits each string into substrings
lista = [a[i:j] for i in range(0, (len(a) - n))
for j in range(n, len(a))]
listb = [b[i:j] for i in range(0, (len(b) - n))
for j in range(n, len(b))]
# compute a list of all substrings that appear in both a and b
matches = []
for element in lista:
if element in listb:
matches.append(element)
matches = list(dict.fromkeys(matches))
return matches
Add a comment
|
1 Answer
You are meant to find a list of all the substrings of len n
. For that, there is no need for separate i
and j
, you could use the slice [i:i+n]
with the correct range
(think how many substrings of length n
are there in a string of length len(a)
).