Step 1: Compare characters at positions (0,0): 'a' and 'a' match, set length to 1
DP matrix partial:
[1,0,0,0,0]
[0,0,0,0,0]
[0,0,0,0,0]
[0,0,0,0,0]
[0,0,0,0,0]
Why: Matching characters start a common substring of length 1
Step 2: Compare characters at positions (1,1): 'b' and 'b' match, length becomes 2
DP matrix partial:
[1,0,0,0,0]
[0,2,0,0,0]
[0,0,0,0,0]
[0,0,0,0,0]
[0,0,0,0,0]
Why: Extending the previous substring by one character
Step 3: Compare characters at positions (2,2): 'c' and 'f' do not match, length reset to 0
DP matrix partial:
[1,0,0,0,0]
[0,2,0,0,0]
[0,0,0,0,0]
[0,0,0,0,0]
[0,0,0,0,0]
Why: Mismatch breaks the substring continuity
Step 4: Compare characters at positions (4,4): 'e' and 'e' match, length is 1
DP matrix partial:
[1,0,0,0,0]
[0,2,0,0,0]
[0,0,0,0,0]
[0,0,0,0,0]
[0,0,0,0,1]
Why: New matching substring starts at the end
Result: Longest common substring length is 2, substring is "ab"