| 1 | Initialize first row and column | (0,0) to (0,6) and (0,0) to (7,0) | N/A | 0 | [Row0: 0 0 0 0 0 0 0]
[Col0: all 0] |
| 2 | Compare s1[1]='A' and s2[1]='B' | (1,1) | A vs B | max(dp[0][1], dp[1][0])=0 | [... dp[1][1]=0 ...] |
| 3 | Compare s1[1]='A' and s2[2]='D' | (1,2) | A vs D | max(dp[0][2], dp[1][1])=0 | [... dp[1][2]=0 ...] |
| 4 | Compare s1[1]='A' and s2[3]='C' | (1,3) | A vs C | max(dp[0][3], dp[1][2])=0 | [... dp[1][3]=0 ...] |
| 5 | Compare s1[1]='A' and s2[4]='A' | (1,4) | A vs A | dp[0][3] + 1 = 1 | [... dp[1][4]=1 ...] |
| 6 | Compare s1[1]='A' and s2[5]='B' | (1,5) | A vs B | max(dp[0][5], dp[1][4])=1 | [... dp[1][5]=1 ...] |
| 7 | Compare s1[1]='A' and s2[6]='A' | (1,6) | A vs A | dp[0][5] + 1 = 1 | [... dp[1][6]=1 ...] |
| 8 | Compare s1[2]='B' and s2[1]='B' | (2,1) | B vs B | dp[1][0] + 1 = 1 | [... dp[2][1]=1 ...] |
| 9 | Compare s1[2]='B' and s2[2]='D' | (2,2) | B vs D | max(dp[1][2], dp[2][1])=1 | [... dp[2][2]=1 ...] |
| 10 | Compare s1[2]='B' and s2[3]='C' | (2,3) | B vs C | max(dp[1][3], dp[2][2])=1 | [... dp[2][3]=1 ...] |
| 11 | Compare s1[2]='B' and s2[4]='A' | (2,4) | B vs A | max(dp[1][4], dp[2][3])=1 | [... dp[2][4]=1 ...] |
| 12 | Compare s1[2]='B' and s2[5]='B' | (2,5) | B vs B | dp[1][4] + 1 = 2 | [... dp[2][5]=2 ...] |
| 13 | Compare s1[2]='B' and s2[6]='A' | (2,6) | B vs A | max(dp[1][6], dp[2][5])=2 | [... dp[2][6]=2 ...] |
| 14 | Continue filling dp table similarly for all i=3 to 7 and j=1 to 6 | (3..7,1..6) | Compare chars | Fill dp[i][j] accordingly | Partial dp table fills up |
| 15 | Final dp[m][n] computed | (7,6) | s1[7]='B', s2[6]='A' | 4 | Full dp table filled, dp[7][6]=4 |
| 16 | Trace back from dp[7][6] to find LCS string | Start at (7,6) | Follow dp values to find matching chars | LCS length=4 | LCS string = "BDAB" |
| 17 | End | N/A | N/A | N/A | LCS length = 4, LCS = "BDAB" |