Input: ‘str1’ = “abcjklp” , ‘str2’ = “acjkp”.
Output: 3
Explanation: The longest common substring between ‘str1’ and ‘str2’ is “cjk”, of length 3.
The First line of the test case contains the string ‘str1’.
The Second line contains the string ‘str2’.
Return an integer representing the length of the longest common substring.
You don’t need to print anything. Just implement the given function.
The basic idea is to recursively try and match characters from ‘str1’ to characters of ‘str2’ and vice versa also.
The steps are as follows:
The basic idea is to start slicing the first string into the substring and match those substrings in the second string.
The steps are as follows:
The basic idea of this approach is to solve the problem iteratively.
Let ‘dp’[i] [j] be our dynamic programming matrix to store the length of LCS of substring ‘str1’[0: i-1] and ‘str2’[0:j-1].
Now, consider the following steps:
Start traversing the ‘str1’ using a variable 'i'.
Create a nested loop and start traversing the ‘str2' using a variable ‘j’.
If either ‘i’ or ‘j’ is zero then ‘dp’[i][j] = 0 because one of the strings is empty.
If ( ‘str1'[i-1] == ‘str2'[j-1] ), which means the current character of both strings matches, then it is optimal to include the current character in the LCS. Therefore, find the LCS of the remaining characters of both strings. ‘dp’[i][j] = ‘dp’[i-1][j-1] + 1
Otherwise, we can not have both ‘str1'[i-1] and ‘str2'[j-1] at the end of the LCS, which means one of them must be ignored. Now there are two possibilities, either we ignore ‘str1'[i-1] and get the LCS of the rest of the characters and vice versa. We should choose the option which maximizes the LCS i.e. ‘dp’[i][j] = max(‘dp’[i-1][j] , ‘dp’[i][j-1])
Now we can get the length of LCS from ‘dp’[n][m] where ‘n’ and ‘m’ are the length of ‘str1’ and ‘str2’ respectively.
Search Pattern (KMP - Algorithm)
Search Pattern (KMP - Algorithm)
Search Pattern (KMP - Algorithm)
Growing String
Growing String
RNA or DNA
RNA or DNA
Count Character Occurrences
Split String