Click here to Skip to main content
15,887,875 members
Articles / Programming Languages / C++
Tip/Trick

Longest Common Suffix In Strings

Rate me:
Please Sign up or sign in to vote.
4.43/5 (4 votes)
12 Feb 2014CPOL2 min read 24.9K   1
There are so many recipies to get the Longest Common Substring from two given strings...

Introduction

Finding the longest common substring (LCS) is one of the most interesting topics in computer algorithms. In total for a string with n characters, there are substrings. That is based on choosing the first and the end of array among (n+1) places in the string. For example, to get substrings of "abc", you need to choose two places among the dashes in :  _a_b_c_

which results in:

We wish to find a maximum length common subsequence of X and Y with length m and n in order. There are a variety of ways to find LCS in two strings X and Y:

  1. A brute-force solution is to have two pointers on each string and start to check if each character on each string matches or not. If it matches continue if not, move pointer one character a head and check the rest if they match or not. This is the easiest but worst algorithm to find the LCS. The time complexity is O(m^2*n^2) . This time is so big and for long strings, this solution is impractical.
  2. Find the substrings of x and check in y if it exists or not. We have  m^2 substrings in x and checking if the substring exist takes O(n) time (using Knuth-Morris-Pratt algorithm). In total, it takes O(n* m^2) 
  3. If you know any other ways, please let me know! It is so much fun to find another way. :) 

 

Recursive Solution

Based on the following fact that LCS(i, j) = LCS ( i - 1 , j - 1) + 1 , where X[i] = Y[j] 

we can recursively find the length of the common string.

 

                   0                                                                         if i = 0 and j = 0

LCS(i, j) =   LCS ( i - 1 , j - 1) + 1                                           if (X[i] = Y[j] )

                   Max ( LCS ( i , j - 1) , LCS ( i - 1 , j - 1))              else

 

By dynamic programming, we can implement the following code in a bottom up manner with time complexity of O(m*n) which is not comparable to other solutions.

C++
//
void LongestCommenSubstring(std::string str1, std::string str2)
{
    const int m = str1.length();
    const int n = str2.length();
    int lcs[m][n];

    for(int i = 0; i < m; ++i)
    {
        for(int j = 0; j < n;++j)
        {
            lcs[i][j] = 0;
        }
    }

    for(int i = 0; i < m; ++i)
    {
        for(int j = 0; j < n;++j)
        {
            if(str1[i] == str2[j])
            {
                if((i-1 >= 0 ) && ( j-1 >= 0))
                {
                    if(str1[i-1] == str2[j-1])
                    {
                        lcs[i][j] = lcs[i-1][j-1]+1;
                    }
                }
            }
            else
            {

                if((i-1 >= 0 ) &&( j-1 >= 0))
                {
                    lcs[i][j] = std::max(lcs[i][j-1], lcs[i-1][j]);
                }
            }
        }
    }
   std::cout << "longest commen substring" << lcs[m-1][n-1];}
}
// 

Remember this code just outputs the length. In order to print the longest substring, you need to store it.

Coding Tip

Always initialize your class members, otherwise you'll get garbage in your output !! Which most of the time is not easy to catch while debugging.

More Challenge?

Using this method, you can find the LCS of more strings just by little modification in the code. :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionconvergent algorithm Pin
b.leclerc19-Feb-14 0:19
b.leclerc19-Feb-14 0:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.