Answer to Question #167133 in Python for prathyusha

Question #167133

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


Trying:
    overlap('ramisgood', 'goodforall')
Expecting:
    'good'
ok
Trying:
    overlap('final', 'final')
Expecting:
    'final'
ok
Trying:
    overlap('toast', 'tst')
Expecting:
    't'
ok
Trying:
    overlap('', '')
Expecting:
    'No overlapping'
ok
Trying:
    overlap('first', '')
Expecting:
    'No overlapping'
ok
Trying:
    overlap('', 'second')
Expecting:
    'No overlapping'
ok
1 items had no tests:
    __main__
1 items passed all tests:
   6 tests in __main__.overlap
6 tests in 2 items.
6 passed and 0 failed.
Test passed.

Expected:

good

Expected: output 2:

No overlapping
1
Expert's answer
2021-03-02T16:35:27-0500
a = 'ramisgood'
b = 'goodforall'

c = 'finally'
d = 'restforall'


def longestSubstringFinder(string1, string2):
    answer = ""
    len1, len2 = len(string1), len(string2)
    for i in range(len1):
        match = ""
        for j in range(len2):
            if (i + j < len1 and string1[i + j] == string2[j]):
                match += string2[j]
            else:
                if (len(match) > len(answer)): answer = match
                match = ""
    if answer == '':
        return 'No overlapping'
    else:
        return answer

print(longestSubstringFinder(a, b))
print(longestSubstringFinder(c, d))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS