Longest common subsequence
Compare the first characters of what's left of both strings. If they match, they're part of the answer — count one and move both on. If not, drop one from either string and keep the longer result. dp[index1][index2] remembers each pair of suffixes.