这是一个基本问题,但是我在任何地方都找不到正确的答案。 我在python中有2个相等长度的字符串,我想查找第二个字符串中是否包含至少n个字符(n可以小于字符串长度的任何数字)(第一个字符串包含相同的顺序)。
例如:
s1 = 'TAGCACTTT'
s2 = 'CGATGATTT'
s3 = 'AGACGGCCT'
n = 6
匹配s1和s2应该返回True,因为s2按顺序包含字母“ AGATTT”,而匹配s1和s3应该返回False,因为只有5个字母匹配。 我如何有效地做到这一点?
答案 0 :(得分:0)
def max_sub(X, Y, m, n):
if m == 0 or n == 0:
return 0;
elif X[m - 1] == Y[n - 1]:
return 1 + max_sub(X, Y, m - 1, n - 1);
else:
return max(max_sub(X, Y, m, n - 1), max_sub(X, Y, m - 1, n))
s1 = 'TAGCACTTT'
s2 = 'CGATGATTT'
s3 = 'AGACGGCCT'
n = 6
# Permutations using library function
from itertools import permutations
# Get all permutations of [s1,s2,s3]
perm = permutations([s1, s2, s3],2)
# pass over the obtained permutations
for i in list(perm):
len_ = max_sub(i[0], i[1], len(i[0]), len(i[1]))
if len_ >= n:
print("For {},{} length of common substring is: {}, True ".format(i[0], i[1], max_sub(i[0], i[1], len(i[0]), len(i[1]))))
else:
print("For {},{} length of common substring is: {}, False ".format(i[0], i[1], max_sub(i[0], i[1], len(i[0]), len(i[1]))))
输出:
For TAGCACTTT,CGATGATTT length of common substring is: 6, True
For TAGCACTTT,AGACGGCCT length of common substring is: 5, False
For CGATGATTT,TAGCACTTT length of common substring is: 6, True
For CGATGATTT,AGACGGCCT length of common substring is: 4, False
For AGACGGCCT,TAGCACTTT length of common substring is: 5, False
For AGACGGCCT,CGATGATTT length of common substring is: 4, False