Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to generate the shortest string from two string

Tags:

python-3.x

I would like to write a function to return the shortest string C from two string, A, B and make sure string A, B is substring of C. But the key is length of A does not have to longer than B ex:

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

I have following function but it seems like it is not correct

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:]
like image 456
sln Avatar asked Feb 09 '19 05:02

sln


People also ask

How do you find the smallest common substring?

Given two strings str1 and str2, the task is to find the length of the shortest string that has both str1 and str2 as subsequences. Examples : Input: str1 = "geek", str2 = "eke" Output: 5 Explanation: String "geeke" has both string "geek" and "eke" as subsequences.

What is a Supersequence of a string?

A string is a common supersequence of strings and if it is a supersequence of both and ; that is, it contains both and as subsequences. For example, "GACCTAGGAACT" is a common supersequence of "ACGT" and "ATAT".

What is shortest common superstring?

A shortest common supersequence (SCS) is a common supersequence of minimal length. In the shortest common supersequence problem, two sequences X and Y are given, and the task is to find a shortest possible common supersequence of these sequences. In general, an SCS is not unique.


1 Answers

You can back up from the end looking for a match like:

Code:

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

Test Code:

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'])
like image 97
Stephen Rauch Avatar answered Sep 23 '22 01:09

Stephen Rauch