Discover how to find the longest matching part between two texts without endless guessing!
Why Longest Common Substring in DSA Typescript?
Imagine you have two long texts and you want to find the longest part that appears exactly the same in both. Doing this by reading and comparing every possible part by hand would take forever!
Manually checking every possible substring is slow and confusing. You might miss some matches or spend hours comparing parts that don't match. It's easy to make mistakes and waste time.
The Longest Common Substring method quickly finds the longest matching part between two texts by using a smart way to compare all parts at once. It saves time and avoids mistakes.
function findLongestCommonSubstringManual(text1: string, text2: string): string {
let longest = '';
for (let i = 0; i < text1.length; i++) {
for (let j = 0; j < text2.length; j++) {
let k = 0;
while (i + k < text1.length && j + k < text2.length && text1[i + k] === text2[j + k]) {
k++;
}
if (k > longest.length) {
longest = text1.substring(i, i + k);
}
}
}
return longest;
}function longestCommonSubstring(text1: string, text2: string): string {
const dp: number[][] = Array(text1.length + 1).fill(0).map(() => Array(text2.length + 1).fill(0));
let maxLength = 0;
let endIndex = 0;
for (let i = 1; i <= text1.length; i++) {
for (let j = 1; j <= text2.length; j++) {
if (text1[i - 1] === text2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
if (dp[i][j] > maxLength) {
maxLength = dp[i][j];
endIndex = i;
}
}
}
}
return text1.substring(endIndex - maxLength, endIndex);
}This lets you quickly find the longest exact matching part between two texts, which helps in many areas like spell checking, DNA analysis, and plagiarism detection.
When checking if two documents share a copied paragraph, the Longest Common Substring helps find the longest copied section automatically, saving hours of manual reading.
Manual comparison is slow and error-prone.
Longest Common Substring uses a smart table to find matches fast.
This method helps in text analysis, biology, and more.
Practice
s1 and s2?Solution
Step 1: Understand substring vs subsequence
A substring is a continuous part of a string, while a subsequence can skip characters.Step 2: Define Longest Common Substring
It finds the longest continuous sequence of characters present in both strings.Final Answer:
The longest sequence of characters that appear contiguously in both strings -> Option CQuick Check:
Longest Common Substring = continuous match [OK]
- Confusing substring with subsequence
- Thinking order doesn't matter
- Counting total common characters instead
s1 and s2?Solution
Step 1: Identify input types
The inputs are two strings, so parameters should bestring.Step 2: Identify output type
The function returns the length of the longest common substring, which is a number.Final Answer:
function longestCommonSubstring(s1: string, s2: string): number -> Option AQuick Check:
Input strings, output number length [OK]
- Using arrays instead of strings as input
- Returning string or array instead of number
- Using void return type mistakenly
function longestCommonSubstring(s1: string, s2: string): number {
const m = s1.length, n = s2.length;
let maxLen = 0;
const dp: number[][] = Array(m + 1).fill(0).map(() => Array(n + 1).fill(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'));Solution
Step 1: Compare characters of 'abcde' and 'abfde'
Matching continuous substrings are 'ab' at start and 'de' at end, both length 2.Step 2: Find longest continuous match
Longest common substring length is 2.Final Answer:
2 -> Option AQuick Check:
Longest common substring length = 2 [OK]
- Counting non-continuous matches
- Confusing longest common subsequence with substring
- Off-by-one errors in indexing
function longestCommonSubstring(s1: string, s2: string): number {
const m = s1.length, n = s2.length;
let maxLen = 0;
const dp: number[][] = Array(m).fill(0).map(() => Array(n).fill(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;
}Solution
Step 1: Check dp array initialization
dp is created with size m x n but accessed with indices up to m and n (1-based), causing out-of-bound errors.Step 2: Correct dp size
dp should be (m+1) x (n+1) to safely use 1-based indexing in loops.Final Answer:
dp array is initialized with wrong size causing index errors -> Option BQuick Check:
dp size must match loop indices [OK]
- Not adding +1 to dp dimensions
- Starting loops at 1 but dp size too small
- Ignoring index out-of-bound runtime errors
s1 = 'xabxac' and s2 = 'abcabxabcd', what is the length of their longest common substring?Solution
Step 1: Identify common substrings
Common substrings include 'abx' and 'abxa'. 'abxa' is length 4 and appears in both strings.Step 2: Confirm longest substring length
No longer continuous substring than length 4 exists in both strings.Final Answer:
4 -> Option DQuick Check:
Longest common substring length = 4 [OK]
- Counting non-continuous matches
- Choosing shorter common substrings
- Confusing substring with subsequence
Ready to prove it?
Pass the test and this pattern moves unknown
