I need to solve a question that asks for code finding the longest common substring between 2 strings:
My code is not running for one test case given below:
17 60
KXCGMTMVVGFQQWSPD
JXZADDUKVLQPKUZJZHWSUTPCAFSYAIBJHAMMEGWBTPQELRNKBLDXGUZGCSEC
The code is given below
//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
    public:
    // This is the recursive approach 
    int longest( string s1 , string s2  , int i,  int j , int & ans ,vector<vector<int>> &dp  )
    {
        if(i<0 && j<0)
        {
            return 1;
        }
        if(i<0|| j<0)
        {
            return 0;
        }
        if(dp[i][j]!=-1)
        {
            return dp[i][j];
        }
        if(s1[i]==s2[j])
        {
            int k = 1+longest(s1,s2 ,i-1 ,j-1 ,ans ,dp);
            ans = max(ans, k);
            dp[i][j]=k ;
            return k ;
        }
        int a =longest(s1, s2 , i-1,j , ans,dp);
        int b = longest(s1,s2 ,i ,j-1,ans,dp) ;
        dp[i][j]=0;
        return 0;
    }
     // Here is the tabulation approach 
    int longestCommonSubstr (string s1, string s2, int n, int m)
    {
        // your code here
        int ans=0;
        vector<vector<int>> dp (s1.length(),vector<int>( s2.length(),-1));
        int k = longest(s1,s2,n-1,m-1,ans,dp);
        return ans;
        /*int ans =0;
        vector<int> row1(s2.length()+1 ,0);
        vector<int> row2(s2.length()+1 , 0);
        for( int i =0;i<n ;i++)
        {
            for( int j =0;j<m ;j++)
            {
                if(s1[i]==s2[j])
                {
                    int k =1+row1[j];
                    ans = max(ans,k);
                    row2[j+1]=k; 
                }
                else
                {
                    row2[j+1]=0;
                }
            }
            row1= row2 ;
        }
        return ans;*/
    }
};
//{ Driver Code Starts.
int main()
{
    int t; cin >> t;
    while (t--)
    {
        int n, m; cin >> n >> m;
        string s1, s2;
        cin >> s1 >> s2;
        Solution ob;
        cout << ob.longestCommonSubstr (s1, s2, n, m) << endl;
    }
}
// Contributed By: Pranay Bansal
// } Driver Code Ends
I have tried but the memoization technique is failing for one of the test cases but the tabulation technique does work. So please refer to the code and correct it.
 
     
    