Python Program To Find Longest Common Substring

Chapter: Python Last Updated: 23-09-2023 03:10:13 UTC

Program:

            /* ............... START ............... */
                

def longest_common_substring(str1, str2):
    m = len(str1)
    n = len(str2)

    # Create a table to store the length of the common substring
    # at each (i, j) position, where i is from str1 and j is from str2.
    dp = [[0] * (n + 1) for _ in range(m + 1)]

    # Variables to store the length and ending position of the longest
    # common substring found so far.
    max_length = 0
    end_position = 0

    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if str1[i - 1] == str2[j - 1]:
                dp[i][j] = dp[i - 1][j - 1] + 1
                if dp[i][j] > max_length:
                    max_length = dp[i][j]
                    end_position = i

    # Extract the longest common substring using the end position.
    longest_common_sub = str1[end_position - max_length:end_position]

    return longest_common_sub

# Example usage:
str1 = "abcdef"
str2 = "xbcdyz"
result = longest_common_substring(str1, str2)
print("Longest Common Substring:", result)

                /* ............... END ............... */
        

Output

When you run the example with "abcdef" and "xbcdyz," it should print:

Longest Common Substring: bcd


Notes:

  • This program defines a function longest_common_substring that takes two input strings str1 and str2 and returns the longest common substring between them. It uses dynamic programming to fill in a 2D table dp, where dp[i][j] represents the length of the common substring ending at str1[i-1] and str2[j-1]. Finally, it extracts the longest common substring using the end_position and max_length variables.

Tags

Python program to find longest common substring #How do you find the longest substring in a string in Python?

Similar Programs Chapter Last Updated
Python Program To Check Whether Element Present In Set Or Not Example Python 04-10-2023
Python Program To Find Maximum And Minimum Number In A Set Python 04-10-2023
Python Program To Check Symmetric Matrix Python 04-10-2023
Python Program To Find Subsets Of A Set Python 04-10-2023
Python Program To Find Power Set Of A Set Python 04-10-2023
Remove All Duplicates From List Python Python 04-10-2023
Python Program To Find Symmetric Difference Of Two Sets Python 27-09-2023
Python Program To Find Common Item From Two Set Python 27-09-2023
Python Program To Get Unique Values From A List Python 27-09-2023
Python Encode And Decode String With Key Python 24-09-2023
Python Simple Encrypt Decrypt String Python 24-09-2023
Python Format String To Specific Length Python 24-09-2023
Python Code To Check If String Contains Substring Python 24-09-2023
Python Program To Find Most Repeated Word In A String Python 23-09-2023
Split String Into Words Python Python 23-09-2023
Remove All Punctuation Python Python 23-09-2023
Python Program To Reverse An Array Python 23-09-2023
Python Program To Find Number Of Palindrome In A String Python 23-09-2023
Python Program To Find Number Of Days In A Given Month And Year Python 22-09-2023
Python Program To Calculate Age Of A Person Python 22-09-2023
Python Code To Get Day Of Week Python 22-09-2023
Python Convert String To Date Without Time Python 22-09-2023
Python Program To Print Current Date And Time In Format dd/mm/yyyy Python 22-09-2023
Python Program To Find Working Days In A Month Python 19-09-2023
Python Code To Change Date Format Python 16-09-2023
Python Program To Calculate Number Of Days Between Two Dates Python 16-09-2023
Python Program To Calculate Age In Years Months And Days Python 16-09-2023
Python Program To Schedule A Job To Run After A Certain Amount Of Time Python 10-08-2023
Python Program To Schedule A Job To Run Randomly Once A Day Python 10-08-2023
Python Program To Schedule A Job To Run Every Hour Python 10-08-2023

1 2 3 4