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:]
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.
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".
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.
You can back up from the end looking for a match like:
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'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With