鉴于我对您的问题的理解:
类型 1
def diff_count(s1, s2):
count = 0
for i in range(len(s1)):
if s1[i] != s2[i]:
count += 1
return count
def diff_filter1(s1, s2, max_count):
return diff_count(s1, s2) < max_count
类型 2(更高效)
def diff_filter2(s1, s2, max_count):
count = 0
i = 0
while i < len(s1) and count < max_count:
if s1[i] != s2[i]:
count += 1
i += 1
return count < max_count
以及Levenshtein 距离的 Python 代码
def LevenshteinDistance(s, t):
len_s = len(s)- 1
len_t = len(t)- 1
if(len_s == 0): return len_t
if(len_t == 0): return len_s
if(s[len_s-1] == t[len_t-1]): cost = 0
else: cost = 1
return min(LevenshteinDistance(s[0:len_s-1], t) + 1,
LevenshteinDistance(s, t[0:len_t-1]) + 1,
LevenshteinDistance(s[0:len_s-1], t[0:len_t-1]) + cost)