Longest Common Substring | Practice | GeeksforGeeks

Given two strings. The task is to find the length of the longest common substring.

Example 1:
Input: S1 = "ABCDGH", S2 = "ACDGHR"
Output: 4
Explanation: The longest common substring
is "CDGH" which has length 4.

Example 2:
Input: S1 = "ABC", S2 "ACB"
Output: 1
Explanation: The longest common substrings
are "A", "B", "C" all having length 1.


Your Task:
You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2.

Expected Time Complexity: O(n*m).
Expected Auxiliary Space: O(n*m).

Constraints:
1<=n, m<=1000
0
kanikadogra762 days ago
int longestCommonSubstr (string S1, string S2, int n, int m)   {      int t[n+1][m+1];      for(int i=0; i<n+1; i++)      {          for(int j=0; j<m+1; j++)          {              if(i==0 || j==0)              {                  t[i][j]=0;              }          }      }            for(int i=1; i<n+1; i++)      {          for(int j=1; j<m+1; j++)          {              if(S1[i-1]==S2[j-1])              {                  t[i][j]=1+t[i-1][j-1];              }              else              {                  t[i][j]=0;              }          }      }            int mx=INT_MIN;      for(int i=0; i<n+1; i++)      {          for(int j=0; j<m+1; j++)          {              mx=max(mx,t[i][j]);          }      }            return mx;          }
0
harshitsinghparmar902421 week ago
import numpy as npclass Solution:   def longestCommonSubstr(self, S1, S2, n, m):       array= [[0 for i in range(n)] for j in range(m)]       for i in range(n):           for j in range(m):               if S1[i]==S2[j]:                   array[i][j]=1+array[i-1][j-1]               else:                   continue       array=np.array(array)       sd=np.amax(array)       return sd
0
anuragshubham341 week ago
class Solution{   int longestCommonSubstr(String S1, String S2, int n, int m){       int[][] dp=new int[n+1][m+1];       int max_v=0;       //for(int[] a:dp)        for(int i=0;i<=n;i++){            for(int j=0;j<=m;j++){                if(i==0)                 dp[i][j]=0;                 if(j==0)                 dp[i][j]=0;            }        }        for(int i=1;i<=n;i++){            for(int j=1;j<=m;j++){                if(S1.charAt(i-1)==S2.charAt(j-1))                  {                      dp[i][j]=1+dp[i-1][j-1];                      max_v=Math.max(max_v,dp[i][j]);                  }                else                    dp[i][j]=0;            }        }        return max_v;   }}
0
nehalagrawal1862 weeks ago
int longestCommonSubstr (string s1, string s2, int n, int m)   {       // your code here       int ma = 0;       int arr[n][m];              for(int i = n-1 ; i>=0 ; i--){           for(int j = m-1; j>=0 ; j--){               if(s1[i]==s2[j]){                   if(i+1<n&&j+1<m){                       arr[i][j] = arr[i+1][j+1] +1;                   }else{                       arr[i][j] = 1;                   }               }               else{                   arr[i][j] = 0;               }               ma = max(ma,arr[i][j]);           }       }       return ma ;   }
+2
arpitjain8043 weeks ago
def longestCommonSubstr(self, S1, S2, n, m):       res=''       i=0       j=0       while i<=(n-1) and j<=(m-1):           if (S1[i]!=S2[j]):               break           res+=S1[i]           i+=1           j+=1       return res 
 
 
what wrong in this
+2
mashhadihossain3 weeks ago
SIMPLE JAVA SOLUTION (0.6/2.2 SEC)
class Solution{   int longestCommonSubstr(String S1, String S2, int n, int m){       int max=0;       for(int i=0;i<n;i++)       {           for(int j=i+1;j<=n;j++)           {               String ans=S1.substring(i,j);               if(S2.contains(ans))               {                   if(ans.length()>max)                   {                       max=ans.length();                   }               }           }       }       return max;   }}
-2
adityagagtiwari1 month ago
If your solution is becoming very complex it probably means your solution is wrong. 
-4
vikassaraimeer19991 month ago
solution without using extra space 
int longestCommonSubstr (string S1, string S2, int n, int m)   {       // your code here       int ans=0;     for(int i=0;i<n;i++){         for(int j=0;j<m;j++){             int count=0;             int n1=i;             int n2=j;             while(n1>=0&&n2>=0){                 if(S1[n1--]==S2[n2--])                 count++;                 else                 break;             }             ans=max(ans,count);         }     }     return ans;   }
if you have any problem with my solution ,you can go through this awesome tutorial  
0
anchalmishra9161 month ago
int longestCommonSubstr (string S1, string S2, int n, int m)   {       // your code here       int dp[n+1][m+1];      for(int i=0;i<=n;i++)      {          for(int j=0;j<=m;j++)          {              if(i==0 || j==0)              {                  dp[i][j]=0;              }          }      }       for(int i=1;i<=n;i++)      {          for(int j=1;j<=m;j++)          {             if(S1[i-1]==S2[j-1])             {                 dp[i][j]=1+dp[i-1][j-1];             }             else             {                 dp[i][j]=0;             }          }      }      int max=INT_MIN;      for(int i=0;i<=n;i++)      {          for(int j=0;j<=m;j++)          {             if(dp[i][j]>max)             {                 max=dp[i][j];             }          }      }      return max;            }};
0
anchalmishra916
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.