It’s not on the LeetCode, but on the LintCode.
first thought
- Use DP, build a matrix dp to represent the maximum number with the substring of a and b.
- If the current chars(a[i], b[j])are equal, the value of current is the value of last position + 1. dp[i][j] = dp[i - 1][j - 1] + 1;
- If a[i] == b[j - 1], dp[i][j] = dp[i - 1][j - 2] + 1;
- If a[i - 1] == b[j], dp[i][j] = dp[i - 2][j - 1] + 1;
- If not the same, just copy the value of the previous one.
solution
|
|
problem
WA.
reason
A completely confused solution.
rethink
- If they’re the same, just plus 1.
- If they are not the same. It’s the maximum length between dp[i][j - 1] and dp[i - 1][j].
modification
|
|