forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdit Distance.java
More file actions
32 lines (29 loc) · 1.01 KB
/
Edit Distance.java
File metadata and controls
32 lines (29 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public int minDistance(String s1, String s2) {
return minDistanceHelperDP(s1, s2, s1.length(), s2.length());
}
private int minDistanceHelperDP(String s1, String s2, int m, int n) {
int[][] dp = new int[m+1][n+1];
for (int i=0;i<=m;i++) {
for (int j=0;j<=n;j++) {
// First substring is empty
if (i == 0) {
dp[i][j] = j;
}
// Second substring is empty
else if (j == 0) {
dp[i][j] = i;
}
// If character are same, value is same as the previous dp array
else if (s1.charAt(i-1) == s2.charAt(j-1)) {
dp[i][j] = dp[i-1][j-1];
}
// If different, then 1 + min
else {
dp[i][j] = 1 + Math.min(dp[i-1][j-1], Math.min(dp[i][j-1], dp[i-1][j]));
}
}
}
return dp[m][n];
}
}