确定字符串B是否可能是删除/添加字符串a的结果



假设我有一个长度为29的基字符串和一个长度为28和30的任意字符串列表。我如何确定这些字符串的数量,这可能是在基本字符串上执行的删除/添加一个字符的结果?

我是用Python写的

让我看看…我会修改Levenshtein距离算法(这里是Python代码),使其仅在添加或删除一个字符的情况下工作。

from functools import partial
from my_distances import **add_delete_distance**
def is_accepted(base_string, alternative_string):
    '''It uses the custom distance algorithm to evaluate (boolean output) if a
    particular alternative string is ok with respect to the base string.'''
    assert type(alternative_string) == str
    len_difference = abs(len(base_string)-len(alternative_string))
    if len_difference == 1 :
        distance = add_delete_distance(base_string, alternative_string)
        if distance == 1:
            return True
    return False
base_string = 'michele'
alternative_strings = ['michel', 'michelle', 'james', 'michela']
print filter(partial(is_accepted, base_string), alternative_string)

你觉得怎么样?

最新更新