Prefix Suffix
Write a program to check the overlapping of one string's suffix with the prefix of another string.Input
The first line of the input will contain a string A.
The second line of the input will contain a string B.Output
The output should contain overlapping word if present else print "No overlapping".Explanation
For example, if the given two strings, A and B, are "ramisgood" "goodforall"
The output should be "good" as good overlaps as a suffix of the first string and prefix of next.
Sample Input 1
ramisgood
goodforall
Sample Output 1
good
Sample Input 2
finally
restforall
Sample Output 2
No overlapping
def overlap2(A, B):
"""Overlapping of the A string's suffix with the prefix of B string
>>> overlap2('ramisgood', 'goodforall')
'good'
>>> overlap2('finally', 'restforall')
'No overlapping'
>>> overlap2('orion', 'orion')
'orion'
>>> overlap2('tora', 'ara')
'a'
>>> overlap2('', '')
'No overlapping'
>>> overlap2('in', '')
'No overlapping'
>>> overlap2('', 'out')
'No overlapping'
"""
lenA = len(A)
for i in range(lenA - min(lenA, len(B)), lenA):
equal = True
for j in range(lenA - i):
if A[i+j] != B[j]:
equal = False
break
if equal:
return A[i:]
return 'No overlapping'
A = input('String A? ')
B = input('String B? ')
print(overlap2(A, B))
if __name__ == '__main__':
import doctest
doctest.testmod()
Comments
Leave a comment