InferenceAI

Enter at least two characters to search.

Start free

Longest Shared Substring

mediumDynamic Programming25 min25 XP

Return the length of the longest contiguous substring that appears in both a and b.

Examples

EXAMPLE WALKTHROUGHfour letters

A guided view of the published example, not a trace of your code.

INPUT["ABCDGH","ACDGHR"]
TASKlongest_shared_substring()
EXPECTED OUTPUT4

Start with the input values shown in this example.

Example 1 of 3

Input: a = "ABCDGH", b = "ACDGHR"

Output: 4

Explanation: four letters

Input: a = "abc", b = "xyz"

Output: 0

Explanation: nothing shared

Input: a = "", b = "abc"

Output: 0

Explanation: empty

Hint 1

dp[i][j] = length of the common suffix of a[:i] and b[:j]; it resets to 0 on a mismatch.

Topics

DSADynamic ProgrammingPython