Longest Common Substring

In this post, we will discover the basic algorithm to find the longest common substring of two strings.

Problem: Given two strings X and Y, find the longest common substring.

Difficulty: Easy

Examples:

1. X = "timetocode", Y = "timerightnow", Result = "time"
2. X = "abcabdef", Y = "abdf", Result = "abd"

Solution: Dynamic Programming
To solve this problem, we need to find all occurrences of all X letters in Y string. To keep intermediate results we can use a two-dimensional array of length X and Y accordingly.

Longest Common Substring

Even now we can see the result. But to make it more clear, let’s add some rules how to fill this table:
1. If X letter is not equal to Y letter then Table[i][j] = 0.
2. If X letter is equal to Y letter then this is already common substring of length 1. If the previous letter was also substring then the length of a new substring is Table[i][j] = Table[i - 1][j - 1] + 1 (prev length + current length).

Longest Common Substring

Obviously, the length of the longest common substring is the maximum value in this table. And the actual substring can be reproduced by previous cells from the cell with maximum value to the cell with a value of one.

Time complexity: O(N^2)
Space complexity: O(|X| * |Y|)

The source code for this problem you may find here. Thank you for reading my blog and have a nice coding!

One thought on “Longest Common Substring

  1. Thank you for algorithm details! This description is much easier for understanding than on Wiki. But I also know longest common subsequence algorithm. It is very similar to longest common substring, am I right?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s