Challenge - 5 Problems
Longest Common Substring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Longest Common Substring Length Calculation
What is the output of the following C# code that calculates the length of the longest common substring between two strings?
DSA C#
using System; class Program { static int LongestCommonSubstring(string s1, string s2) { int maxLen = 0; int[,] dp = new int[s1.Length + 1, s2.Length + 1]; for (int i = 1; i <= s1.Length; i++) { for (int j = 1; j <= s2.Length; j++) { if (s1[i - 1] == s2[j - 1]) { dp[i, j] = dp[i - 1, j - 1] + 1; if (dp[i, j] > maxLen) maxLen = dp[i, j]; } } } return maxLen; } static void Main() { Console.WriteLine(LongestCommonSubstring("ababc", "babca")); } }
Attempts:
2 left
💡 Hint
Trace the matching characters and their consecutive lengths.
✗ Incorrect
The longest common substring between "ababc" and "babca" is "babc" which has length 4.
❓ Predict Output
intermediateLongest Common Substring Result String
What substring does the following C# code print as the longest common substring between two strings?
DSA C#
using System; class Program { static string LongestCommonSubstring(string s1, string s2) { int maxLen = 0, endIndex = 0; int[,] dp = new int[s1.Length + 1, s2.Length + 1]; for (int i = 1; i <= s1.Length; i++) { for (int j = 1; j <= s2.Length; j++) { if (s1[i - 1] == s2[j - 1]) { dp[i, j] = dp[i - 1, j - 1] + 1; if (dp[i, j] > maxLen) { maxLen = dp[i, j]; endIndex = i - 1; } } } } return s1.Substring(endIndex - maxLen + 1, maxLen); } static void Main() { Console.WriteLine(LongestCommonSubstring("abcdef", "zabcf")); } }
Attempts:
2 left
💡 Hint
Look for the longest continuous matching substring.
✗ Incorrect
The longest common substring between "abcdef" and "zabcf" is "abc" with length 3.
❓ Predict Output
advancedLongest Common Substring Length with Empty String
What is the output of this C# code when one input string is empty?
DSA C#
using System; class Program { static int LongestCommonSubstring(string s1, string s2) { int maxLen = 0; int[,] dp = new int[s1.Length + 1, s2.Length + 1]; for (int i = 1; i <= s1.Length; i++) { for (int j = 1; j <= s2.Length; j++) { if (s1[i - 1] == s2[j - 1]) { dp[i, j] = dp[i - 1, j - 1] + 1; if (dp[i, j] > maxLen) maxLen = dp[i, j]; } } } return maxLen; } static void Main() { Console.WriteLine(LongestCommonSubstring("", "abc")); } }
Attempts:
2 left
💡 Hint
Consider what happens when one string has no characters.
✗ Incorrect
If one string is empty, there can be no common substring, so the length is 0.
🧠 Conceptual
advancedTime Complexity of Longest Common Substring Algorithm
What is the time complexity of the dynamic programming solution to find the longest common substring of two strings of lengths m and n?
Attempts:
2 left
💡 Hint
Consider the nested loops over both strings.
✗ Incorrect
The algorithm uses a 2D array and nested loops over both strings, resulting in O(m * n) time.
❓ Predict Output
expertLongest Common Substring with Repeated Characters
What is the output of this C# code that finds the longest common substring between two strings with repeated characters?
DSA C#
using System; class Program { static string LongestCommonSubstring(string s1, string s2) { int maxLen = 0, endIndex = 0; int[,] dp = new int[s1.Length + 1, s2.Length + 1]; for (int i = 1; i <= s1.Length; i++) { for (int j = 1; j <= s2.Length; j++) { if (s1[i - 1] == s2[j - 1]) { dp[i, j] = dp[i - 1, j - 1] + 1; if (dp[i, j] > maxLen) { maxLen = dp[i, j]; endIndex = i - 1; } } } } return s1.Substring(endIndex - maxLen + 1, maxLen); } static void Main() { Console.WriteLine(LongestCommonSubstring("aaabaaa", "baaaab")); } }
Attempts:
2 left
💡 Hint
Check the longest continuous matching substring carefully.
✗ Incorrect
The longest common substring between "aaabaaa" and "baaaab" is "aaab" which has length 4.
