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


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!