Edit Distance
Logical Breakdown
- [ ] Subproblem Identification:
- [ ] Optimal Substructure:
- [ ] Constraint Handling: (e.g., Modulo \(10^9 + 7\))
- [ ] Optimization: (Matrix Exponentiation if 'A' is huge)
Mathematical Rigor
State Definition
Let \(dp[i]\) be the state for the \(i\)-th subproblem.
Recurrence Relation
Visualization
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Iterative DP | \(O(N)\) | \(O(1)\) |
Code Reference
package com.dsa.dynamic_programming;
import java.util.*;
/ * Problem: Edit Distance * Group: 11. Dynamic Programming */ / * ============================================================================================ * PROBLEM: Edit Distance (Levenshtein Distance) * ============================================================================================ * * --------------------- * 1. Problem Description * --------------------- * Find the minimum number of operations required to convert string A to string B. * Operations allowed: * 1. Insert a character * 2. Delete a character * 3. Replace a character * * --------------------- * 2. Logical Breakdown * --------------------- * Let dp[i][j] be the min operations to convert A[0...i-1] to B[0...j-1]. * * Transitions: * If A[i-1] == B[j-1]: No operation needed for this char. * dp[i][j] = dp[i-1][j-1] * * If A[i-1] != B[j-1]: We take the minimum of 3 possible moves + 1 cost: * - Insert: dp[i][j-1] (Insert char from B, move j back) * - Delete: dp[i-1][j] (Delete char from A, move i back) * - Replace: dp[i-1][j-1] (Replace A's char with B's) * dp[i][j] = 1 + min(Insert, Delete, Replace) * * Base Cases: * dp[0][0] = 0 * dp[i][0] = i (Deleting all characters from A to match empty B) * dp[0][j] = j (Inserting all characters of B into empty A) * * --------------------- * 3. Complexity * --------------------- * Time: O(N * M) * Space: O(N * M) * * ============================================================================================ */ public class EditDistance { public int solve(int A) { // TODO: Implement solution return 0; }
public int minDistance(String horse, String ros) {
return 1;
}
}