You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We define $f[i][j]$ as the minimum number of operations to convert $word1$ of length $i$ to $word2$ of length $j$. $f[i][0] = i$, $f[0][j] = j$, $i \in [1, m], j \in [0, n]$.
55
+
56
+
We consider $f[i][j]$:
57
+
58
+
- If $word1[i - 1] = word2[j - 1]$, then we only need to consider the minimum number of operations to convert $word1$ of length $i - 1$ to $word2$ of length $j - 1$, so $f[i][j] = f[i - 1][j - 1]$;
59
+
- Otherwise, we can consider insert, delete, and replace operations, then $f[i][j] = \min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1$.
60
+
61
+
Finally, we can get the state transition equation:
Copy file name to clipboardexpand all lines: solution/1100-1199/1143.Longest Common Subsequence/README_EN.md
+30-2
Original file line number
Diff line number
Diff line change
@@ -49,16 +49,22 @@
49
49
50
50
## Solutions
51
51
52
-
Dynamic programming.
52
+
**Solution 1: Dynamic Programming**
53
+
54
+
Let $f[i][j]$ be the length of the longest common subsequence of $text1$ and $text2$ with length $i$ and $j$, respectively. The answer is $f[m][n]$, where $m$ and $n$ are the lengths of $text1$ and $text2$, respectively.
55
+
56
+
If the $i$-th character of $text1$ and the $j$-th character of $text2$ are the same, then $f[i][j] = f[i - 1][j - 1] + 1$; if the $i$-th character of $text1$ and the $j$-th character of $text2$ are different, then $f[i][j] = max(f[i - 1][j], f[i][j - 1])$. That is, the state transition equation is:
0 commit comments