Original question
Example:
input: strings “AGGTAB” and “GXTXAYB”
output: 4
Two cases:
string S and T, m = S.length + 1, n = T.length + 1
dp[i][j] records the length of longest common subsequence between S[0..i] and T[0..j]
- S[i] == T[j] then dp[i][j] = dp[i-1][j-1] + 1
- S[i] != T[j] then dp[i][j] = max(dp[i-1][j], dp[i][j-1])
1 |
int (string S, string T){ |
LC 583. Delete Operation for Two Strings
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.
Input: “sea”, “eat”
Output: 2
Explanation: You need one step to make “sea” to “ea” and another step to make “eat” to “ea”.
The question can be inverted to find the longest common subsequence.
1 |
int minDistance(string word1, string word2) { |
近期评论