Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

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

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Longest Common Substring

...