如何从两个字符串生成最短的字符串

时间:2019-02-09 05:04:23

标签: python-3.x

我想编写一个函数以从两个字符串A,B中返回最短的字符串C,并确保字符串A,B是C的子字符串。但是关键是A的长度不必大于B 例如:

  

A:'abcd',B:'cde'=> C:'abcde'#c,d被重复
  A:'abcd',B:'ecd'=> C:'abcdecd'#没有重复的字符,所以C是A + B
  A:'abc',B:'cdeab'=> C:'cdeabc'
  A:'bce',B:'eabc'=> C:'eabce'#eabcd的长度为5,bceabc的长度为6
  A:'',B:'abc'=> C:'abc'
  A:'abc',B:''=> C:'abc'

我具有以下功能,但似乎不正确

def checksubstring(A, B):
    if not A or not B: return A if not B else B
    index, string = 0, ''
    for i, c in enumerate(A):
        index = index + 1 if c == B[index] else 0
        string += c
    return string + B[index:]

2 个答案:

答案 0 :(得分:6)

您可以从头开始备份,寻找类似的匹配项:

代码:

def shortest_substring(a, b):

    def checksubstring(a, b):
        if not a or not b:
            return b or a

        for i in range(1, len(b)):
            if a[len(a) - i:] == b[:i]:
                return a + b[i:]
        return a + b

    x = checksubstring(a, b)
    y = checksubstring(b, a)
    return x if len(x) <= len(y) else y

测试代码:

results = [
    {'A': 'abcd', 'B': 'cde', 'C': 'abcde'},
    {'A': 'abcd', 'B': 'ecd', 'C': 'abcdecd'},
    {'A': 'abc', 'B': 'cdeab', 'C': 'cdeabc'},
    {'A': 'bce', 'B': 'eabc', 'C': 'eabce'},
    {'A': '', 'B': 'abc', 'C': 'abc'},
    {'A': 'abc', 'B': '', 'C': 'abc'},
]

for result in results:
    assert result['C'] == shortest_substring(result['A'], result['B'])

答案 1 :(得分:0)

您必须检查A,B和B,A,然后检查其结果:

def checksubstring(A, B):
    if not A or not B: return A if not B else B
    index, string = 0, ''
    for i, c in enumerate(A):
        index = index + 1 if c == B[index] else 0
        string += c
    return string + B[index:]


def test(A, B):
    s1 = checksubstring(A, B)
    s2 = checksubstring(B, A)
    if len(s1) > len(s2):
        return s2
    else:
        return s1


print(test('abcd', 'cde'))  # = > C: 'abcde' # c,d is duplicated
print(test('abcd', 'ecd'))  #  = > C: 'abcdecd' #no character duplicated so C is A + B
print(test('abc', 'cdeab')) # = > C: 'cdeabc'
print(test('bce', 'eabc'))  # = > C: 'eabce' #length of eabcd is 5, length of bceabc is 6
print(test('', 'abc'))      # = > C: 'abc'
print(test('abc', ''))      # = > C: 'abc'