It is the problem of finding the longest sequence of characters that appears in the same order and contiguously in two given strings.
Click to reveal answer
intermediate
How does the Longest Common Substring differ from Longest Common Subsequence?
Longest Common Substring requires the characters to be contiguous in both strings, while Longest Common Subsequence allows characters to be non-contiguous but in order.
Click to reveal answer
intermediate
What data structure is commonly used to solve the Longest Common Substring problem efficiently?
A 2D array (matrix) is used to store lengths of common substrings ending at each pair of indices in the two strings.
Click to reveal answer
intermediate
In the dynamic programming solution, what does the cell dp[i][j] represent?
It represents the length of the longest common substring ending at position i-1 in the first string and j-1 in the second string.
Click to reveal answer
beginner
What is the time complexity of the dynamic programming approach to Longest Common Substring?
The time complexity is O(m * n), where m and n are the lengths of the two input strings.
Click to reveal answer
What does the Longest Common Substring problem find?
AThe longest sequence of characters in order but not necessarily contiguous
BThe longest substring unique to the first string
CThe shortest substring in the first string
DThe longest sequence of contiguous characters common to both strings
✗ Incorrect
Longest Common Substring finds the longest contiguous sequence common to both strings.
Which data structure is primarily used in the dynamic programming solution for Longest Common Substring?
A2D array (matrix)
BQueue
CLinked list
DStack
✗ Incorrect
A 2D array stores lengths of common substrings ending at each index pair.
If dp[i][j] = 0 in the dynamic programming table, what does it mean?
ALongest common substring length is maximum here
BCharacters at positions i-1 and j-1 are equal
CCharacters at positions i-1 and j-1 are different
DStrings are empty
✗ Incorrect
dp[i][j] = 0 means characters at those positions do not match, so no common substring ends there.
What is the initial value of dp[i][0] and dp[0][j] in the dynamic programming table?
A0
B1
CLength of the strings
DUndefined
✗ Incorrect
First row and first column are initialized to 0 because no substring can end before the start of a string.
What is the time complexity of the Longest Common Substring dynamic programming solution?
AO(m + n)
BO(m * n)
CO(m^2 * n^2)
DO(log(m + n))
✗ Incorrect
The solution fills an m by n matrix, so time complexity is O(m * n).
Explain how the dynamic programming table is built to find the Longest Common Substring.
Think about how matching characters extend substrings and mismatches reset length.
You got /5 concepts.
Describe the difference between Longest Common Substring and Longest Common Subsequence.
Focus on the word 'contiguous' versus 'in order'.
You got /4 concepts.
Practice
(1/5)
1. What does the Longest Common Substring problem find between two strings?
easy
A. The shortest substring that contains all characters from both strings
B. The longest sequence of characters that appear anywhere in both strings, not necessarily consecutively
C. The total number of common characters between two strings
D. The longest sequence of characters that appear consecutively in both strings
Solution
Step 1: Understand the problem definition
The Longest Common Substring problem looks for the longest continuous sequence of characters shared by both strings.
Step 2: Differentiate from similar problems
Unlike Longest Common Subsequence, the substring must be consecutive characters, not scattered.
Final Answer:
The longest sequence of characters that appear consecutively in both strings -> Option D
Quick Check:
Longest Common Substring = consecutive matching characters [OK]
Hint: Look for continuous matching characters, not scattered ones [OK]
Common Mistakes:
Confusing substring with subsequence
Thinking order does not matter
Ignoring continuity requirement
2. Which of the following JavaScript code snippets correctly initializes a 2D array for dynamic programming in Longest Common Substring?
easy
A. const dp = Array(m + 1).fill(Array(n + 1).fill(0));
B. const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
C. const dp = new Array(m + 1).fill(new Array(n + 1).fill(0));
D. const dp = Array(m + 1).map(Array(n + 1).fill(0));
Solution
Step 1: Understand 2D array initialization in JavaScript
Using Array.from with a mapping function creates independent inner arrays, avoiding shared references.
Step 2: Analyze each option
Options B and C use fill with arrays, causing all rows to reference the same inner array. const dp = Array(m + 1).map(Array(n + 1).fill(0)); passes Array(n + 1).fill(0) directly to map, which expects a function and causes TypeError.
Final Answer:
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); -> Option B
Quick Check:
Use Array.from with function for independent rows [OK]
Hint: Use Array.from with function to avoid shared inner arrays [OK]
Common Mistakes:
Using fill with arrays causing shared references
Using map on uninitialized arrays
Not initializing inner arrays properly
3. What is the output of the following JavaScript code?
function longestCommonSubstring(s1, s2) {
const m = s1.length, n = s2.length;
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
let maxLen = 0;
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (s1[i - 1] === s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
maxLen = Math.max(maxLen, dp[i][j]);
}
}
}
return maxLen;
}
console.log(longestCommonSubstring('abcde', 'abfde'));
medium
A. 2
B. 3
C. 4
D. 1
Solution
Step 1: Trace matching characters between 'abcde' and 'abfde'
Matching substrings are 'ab' at start and 'de' at end. Both have length 2.
Step 2: Identify longest common substring length
Longest common substring length is 2, from either 'ab' or 'de'.
Final Answer:
2 -> Option A
Quick Check:
Longest common substring length = 2 [OK]
Hint: Check continuous matching parts, not total matches [OK]
Common Mistakes:
Counting non-continuous matches
Confusing substring length with subsequence length
Off-by-one errors in indexing
4. The following code attempts to find the longest common substring length but has a bug. What is the bug?
function lcsLength(s1, s2) {
const m = s1.length, n = s2.length;
const dp = Array(m + 1).fill(Array(n + 1).fill(0));
let maxLen = 0;
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (s1[i - 1] === s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
maxLen = Math.max(maxLen, dp[i][j]);
}
}
}
return maxLen;
}
medium
A. dp array rows share the same inner array causing incorrect updates
B. Loop indices start from 1 instead of 0 causing index errors
C. The maxLen variable is not updated inside the loop
D. The function returns dp[m][n] instead of maxLen
Solution
Step 1: Analyze dp array initialization
Using fill with an array causes all rows to reference the same inner array, so updates affect all rows.
Step 2: Understand impact on dp updates
Because rows share the same array, dp[i][j] updates overwrite other rows, corrupting the dp table and results.
Final Answer:
dp array rows share the same inner array causing incorrect updates -> Option A
Quick Check:
Shared inner arrays cause dp corruption [OK]
Hint: Avoid fill with arrays; use Array.from with function [OK]
Common Mistakes:
Using fill for 2D arrays causing shared references
Ignoring dp table corruption
Misunderstanding loop indices
5. Given two strings s1 = 'xabxac' and s2 = 'abcabxabcd', what is the longest common substring and its length?
hard
A. 'xab', length 3
B. 'abx', length 3
C. 'abxa', length 4
D. 'abc', length 3
Solution
Step 1: Identify common substrings between s1 and s2
Check substrings of s1 in s2: 'xab' appears, 'abx' appears, 'abxa' appears in s2 starting at index 4.
Step 2: Find the longest continuous match
'abxa' is length 4 and appears in both strings continuously, longer than other matches.
Final Answer:
'abxa', length 4 -> Option C
Quick Check:
Longest common substring = 'abxa' length 4 [OK]
Hint: Check all continuous matches, pick longest [OK]