Problem · Dynamic Programming
Longest Common Substring
Learn this problemProblem statement
Given strings first and second, return the length of their longest common substring.
A substring occupies consecutive positions in its original string. If the strings share no character, return 0.
Function
longestCommonSubstring(first: String, second: String) → intExamples
Example 1
first = "ABABC"second = "BABCA"return = 4BABC appears consecutively in both strings.
Example 2
first = "abc"second = "def"return = 0The strings share no character.
Constraints
0 <= first.length, second.length <= 3000.- Both strings contain only printable ASCII characters.
- An empty string has no non-empty substring.