0
0
DSA Cprogramming~10 mins

Longest Common Substring in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Longest Common Substring
Start with two strings
Create 2D table of size (len1+1) x (len2+1)
Initialize all cells to 0
For each character pair (i,j)
If chars match: table[i
Update max length and position if needed
After filling table, extract substring from max position
Return longest common substring
Done
We build a table to track matching characters between two strings, updating lengths of common substrings and remembering the longest found.
Execution Sample
DSA C
char* longestCommonSubstring(char* s1, char* s2) {
  int len1 = strlen(s1), len2 = strlen(s2);
  int table[len1+1][len2+1];
  int maxLen = 0, endPos = 0;
  for (int i = 0; i <= len1; i++)
    for (int j = 0; j <= len2; j++)
      table[i][j] = 0;
  for (int i = 1; i <= len1; i++) {
    for (int j = 1; j <= len2; j++) {
      if (s1[i-1] == s2[j-1]) {
        table[i][j] = table[i-1][j-1] + 1;
        if (table[i][j] > maxLen) {
          maxLen = table[i][j];
          endPos = i;
        }
      } else {
        table[i][j] = 0;
      }
    }
  }
  char* result = malloc(maxLen + 1);
  strncpy(result, s1 + endPos - maxLen, maxLen);
  result[maxLen] = '\0';
  return result;
}
This code finds the longest substring common to both input strings by building a table of matching character lengths.
Execution Table
StepOperationIndices (i,j)Chars ComparedTable[i][j]Max LengthEnd PositionVisual State (Table snippet)
1Initialize table---00All zeros
2Compare chars(1,1)'a' vs 'b'000table[1][1]=0
3Compare chars(1,2)'a' vs 'a'111table[1][2]=1
4Compare chars(1,3)'a' vs 'c'011table[1][3]=0
5Compare chars(2,1)'b' vs 'b'111table[2][1]=1
6Compare chars(2,2)'b' vs 'a'011table[2][2]=0
7Compare chars(2,3)'b' vs 'c'011table[2][3]=0
8Compare chars(3,1)'c' vs 'b'011table[3][1]=0
9Compare chars(3,2)'c' vs 'a'011table[3][2]=0
10Compare chars(3,3)'c' vs 'c'111table[3][3]=1
11Compare chars(4,1)'d' vs 'b'011table[4][1]=0
12Compare chars(4,2)'d' vs 'a'011table[4][2]=0
13Compare chars(4,3)'d' vs 'c'011table[4][3]=0
14Compare chars(5,1)'e' vs 'b'011table[5][1]=0
15Compare chars(5,2)'e' vs 'a'011table[5][2]=0
16Compare chars(5,3)'e' vs 'c'011table[5][3]=0
17End of loops---11Longest substring length=1, ends at s1 index 1
18Extract substring-----Substring: 'a'
💡 All character pairs compared; longest common substring length found and extracted.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 10After Step 17Final
maxLen011111
endPos011111
table[1][2]011111
table[2][1]001111
table[3][3]000111
Key Moments - 3 Insights
Why do we start filling the table from index 1 instead of 0?
Because table[0][*] and table[*][0] represent empty prefixes, initialized to zero to handle boundary cases. See execution_table rows 2-3 where i and j start at 1.
Why do we add 1 to table[i-1][j-1] when characters match?
Because a matching character extends the previous common substring by one. This is shown in execution_table row 3 where table[1][2] = table[0][1] + 1 = 1.
How do we know which substring is the longest after filling the table?
We track maxLen and endPos during filling. The longest substring ends at endPos in s1 with length maxLen, as shown in execution_table row 17.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the value of table[1][2] and what does it represent?
A2, sum of previous matches
B0, no match at these indices
C1, length of common substring ending at s1[0] and s2[1]
DUndefined, not calculated yet
💡 Hint
Refer to execution_table row 3 under 'Table[i][j]' and 'Chars Compared'
At which step does the algorithm update maxLen for the first time?
AStep 2
BStep 3
CStep 5
DStep 10
💡 Hint
Check execution_table rows where maxLen changes from 0 to 1
If the input strings were 'abc' and 'abc', what would be the maxLen after filling the table?
A3
B1
C2
D0
💡 Hint
Longest common substring length equals the full string length if both strings are identical
Concept Snapshot
Longest Common Substring:
- Use 2D table of size (len1+1) x (len2+1)
- Initialize all cells to 0
- For each i,j: if s1[i-1]==s2[j-1], table[i][j] = table[i-1][j-1] + 1
- Track max length and end position
- Extract substring from s1 using end position and max length
Full Transcript
The Longest Common Substring algorithm compares two strings by building a 2D table. Each cell table[i][j] stores the length of the longest common substring ending at s1[i-1] and s2[j-1]. We initialize the table with zeros. For each character pair, if they match, we add 1 to the value from the previous diagonal cell. We keep track of the maximum length found and the position where it ends in s1. After filling the table, we extract the substring from s1 using the recorded position and length. This method efficiently finds the longest substring shared by both strings.