Technology and programming languages are constantly evolving. However, what remains the same in every programming language is its basics or fundamental concepts. One such basic concept in every programming language is...
Technology and programming languages are constantly evolving. However, what remains the same in every programming language is its basics or fundamental concepts. One such basic concept in every programming language is String.
A string is defined as a data type that is used to store a sequence of characters. Seems easy, right?
Let’s be honest, the string is a huge concept in itself. You can perform multiple operations on a string which makes it one of the most-asked concepts in any coding interview or exam.
One of the most-talked concepts related to strings is substrings. A substring is simply a sequence of characters present in a string in a continuous pattern.
Here, we are going to talk about one very common coding problem related to substrings, ie, how to find the longest common substring.
But before understanding the problem, let’s discuss in detail what a substring is.
As the name suggests, a substring is simply a part of the original string. This substring is a continuous sequence of characters whose length is less than or equal to the length of the original string.
To understand the same, consider the following example:
If the given string is A= “efghij”
The substrings of this given string will be e, f, g, h, i, j, ef, gh, ij, efg, fgh, ghi, hij, efghij and more.
Here, you will be given two strings. You need to write code to find a common substring between the two strings which has the longest number of elements.
For instance, you are given the following strings:
String 1= "apple"
and String 2="ape"
The longest common substring between the two given strings is ap.
To find the longest common substring of given strings, you can use the following methods:
The most basic approach of finding the longest common substring is to check all the possible substrings present in string 1. You will then have to check if each of the substrings of string1 is a substring of string2 or not.
If substrings 1 and 2 are the same, you need to update the length of the common substring. To do this, you will have to fix the beginning points in the strings. At the same time, you will have to check the length of the substring generated for every possible pair of the beginning indices.
Time complexity: The time complexity of this method is calculated as O(N*M* max(N, M).
Space complexity: The space complexity of this method is calculated as O(1).
The next method that you can use to find the longest common substring of the given is using dynamic programming. In this process, we follow the idea of finding the longest common suffix for all the substrings possible for both given sequences.
If i==0 and j==0, then dp[i][j]=0
If str1[i]== str2[j], then dp[i][j]= 1+ dp[i-1][j-1]
Time Complexity: The time complexity of this method is calculated as O(n*m).
Space complexity: The space complexity of this method is calculated as O(n*m).
In this approach, the main idea is to match all the characters of given sequences again and again. If characters match, you need to maximise the length.
Most people may end up getting confused between a common substring and the longest consecutive subsequence problem.
In the longest consecutive subsequence problem, you will be given an array consisting of integer values. You will have to determine the longest subsequence of the array in such a way that the subsequence elements are consecutive in nature.
When it comes to problems related to strings, the longest common substring problem is commonly asked in many interviews and exams.
The longest common substring problem can be resolved using three methods. However, not every solution or method is feasible for everyone.
Therefore, before solving the problem, make sure to go through all the possible methods to resolve the problem and choose the optimal solution.
Can’t donate? Please share. Even a quick share on Facebook can help.
The average share raises $97.