Open In App

Python Program to Check Overlapping Prefix – Suffix in Two Lists

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given 2 Strings, our task is to check overlapping of one string’s suffix with prefix of other string. 

Input : test_str1 = "Gfgisbest", test_str2 = "bestforall"
Output : best

Explanation : best overlaps as suffix of first string and prefix of next.

Input : test_str1 = "Gfgisbest", test_str2 = "restforall"
Output : ''

Explanation : No overlapping.

Method : Using loop + slicing + startswith()

In this, we increment the first list and slice till list end and keep comparing with the prefix substring of other string using startswith(). In this, the word that occurs at end of string is compared with once with prefix of 2nd string. 

Python3




# Python3 code to demonstrate working of
# Overlapping Prefix - Suffix in Two Lists
# Using loop + slicing + startswith()
import re
 
# initializing strings
test_str1 = "Gfgisbest"
test_str2 = "bestforall"
 
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
 
res = ''
for char in range(len(test_str1)):
 
    # using startswith() to get prefix
    if test_str2.startswith(test_str1[char:]):
        res = test_str1[char:]
        break
 
# printing result
print("Overlapped String  : " + str(res))


Output:

The original string 1 is : Gfgisbest
The original string 2 is : bestforall
Overlapped String  : best

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2: Using reduce() and lambda() method

Approach:

  1. Initialize the two input strings test_str1 and test_str2.
  2. Use reduce() function along with lambda function to iterate through the indices of test_str1 and get the overlapping prefix-suffix string by checking whether the suffix of test_str1 starting at index i is present at the beginning of test_str2. If yes, then update the result string to be the suffix of test_str1 starting at index i, else return the current result string.
  3. Initialize the result string to be an empty string.
  4. Print the overlapped string obtained from step 2.

Python3




from functools import reduce
#initializing strings
test_str1 = "Gfgisbest"
test_str2 = "bestforall"
 
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
 
res = reduce(lambda s, i: test_str1[i:] if test_str2.startswith(test_str1[i:]) else s, range(len(test_str1)), '')
 
#printing result
print("Overlapped String  : " + str(res))


Output

The original string 1 is : Gfgisbest
The original string 2 is : bestforall
Overlapped String  : best

Time complexity: O(n^2), where n is the length of the input string test_str1. This is because we are iterating through all the indices.
Auxiliary Space: O(1), as we are not using any extra data structure to store the intermediate results.

Method 3:  use the KMP algorithm

  1. Initialize two strings test_str1 and test_str2 with the values “Gfgisbest” and “bestforall”, respectively.
  2. Print the original values of the two strings using print() statements.
  3. Create an empty string res to store the overlapped string.
  4. Iterate through each character of test_str1 using a for loop and the range() function. The loop variable char will take on values from 0 to the length of test_str1 – 1.
  5. Use the startswith() method to check if test_str2 starts with a suffix of test_str1 that begins with the current character char. If it does, set the value of res to be that suffix, which represents the overlapped string. 
  6. Then break out of the loop, since we only need to find the first such suffix that works.
  7. Print the overlapped string using a print() statement, along with the message “Overlapped String : “.
     

Python3




# Python3 code to demonstrate working of
# Overlapping Prefix - Suffix in Two Lists
# Using loop + slicing + startswith()
 
# initializing strings
test_str1 = "Gfgisbest"
test_str2 = "bestforall"
 
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
 
res = ''
for char in range(len(test_str1)):
 
    # using startswith() to get suffix
    if test_str2.startswith(test_str1[char:]):
        res = test_str1[char:]
        break
 
# printing result
print("Overlapped String  : " + str(res))


Output

The original string 1 is : Gfgisbest
The original string 2 is : bestforall
Overlapped String  : best

Time complexity: O(m+n), where m and n are the lengths of the two input strings, as the KMP algorithm has a linear-time complexity.

Auxiliary space: O(m), where m is the length of the pattern string used in the KMP algorithm, as we need to store the prefix array of the pattern string.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads