- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringChange.java
More file actions
Latest commit
38 lines (33 loc) · 1.14 KB
/
Copy pathStringChange.java
File metadata and controls
38 lines (33 loc) · 1.14 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
33
34
35
36
37
38
publicclassStringChange {
publicstaticintminDistance(Strings1, Strings2) {
intm = s1.length();
intn = s2.length();
// Create a table to store the minimum edit distances
int[][] dp = newint[m + 1][n + 1];
// Initialize the table
for (inti = 0; i <= m; i++) {
dp[i][0] = i;
}
for (intj = 0; j <= n; j++) {
dp[0][j] = j;
}
// Fill the table using dynamic programming
for (inti = 1; i <= m; i++) {
for (intj = 1; j <= n; j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + Math.min(dp[i - 1][j - 1], Math.min(dp[i][j - 1], dp[i - 1][j]));
}
}
}
// Return the minimum edit distance
returndp[m][n];
}
publicstaticvoidmain(String[] args) {
Strings1 = "sitting";
Strings2 = "kitten";
intminDistance = minDistance(s1, s2);
System.out.println("Minimum number of operations required: " + minDistance);
}
}