Fetching algorithms...
| a 0 | c 1 | e 2 | ∅ 3 | |
|---|---|---|---|---|
| a 0 | 0 | 0 | 0 | 0 |
| b 1 | 0 | 0 | 0 | 0 |
| c 2 | 0 | 0 | 0 | 0 |
| d 3 | 0 | 0 | 0 | 0 |
| e 4 | 0 | 0 | 0 | 0 |
| ∅ | 0 | 0 | 0 | 0 |
Initialize a 2D array dp of size (m+1) x (n+1) with all values set to 0. dp[i][j] will store the LCS of text1[i:] and text2[j:].
LCS can be solved by comparing characters from the end of both strings.
If characters match, the LCS length increases by 1 plus the LCS of the remaining suffixes: `1 + dp[i+1][j+1]`.
If they mismatch, the LCS length is the maximum value found by either skipping `text1[i]` or `text2[j]`: `max(dp[i][j+1], dp[i+1][j])`.