-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditDistance.java
More file actions
47 lines (44 loc) · 1.49 KB
/
EditDistance.java
File metadata and controls
47 lines (44 loc) · 1.49 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
39
40
41
42
43
44
45
46
47
/*
Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.
You have the following three operations permitted on a word:
Insert a character
Delete a character
Replace a character
Example 1:
Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')
*/
class Solution {
int dp[][];
public int minDistance(String word1, String word2) {
dp = new int[word1.length() + 1][word2.length() + 1];
for(int i = 0;i < dp.length;i++){
for(int j = 0;j < dp[0].length;j++){
dp[i][j] = Integer.MAX_VALUE;
}
}
// storing base case ie if either string is empty then the length of other word becomes min no of operation
for(int i = 0; i <= word1.length(); i++) {
dp[i][word2.length()] = word1.length() - i;
}
for(int j = 0; j <= word2.length(); j++) {
dp[word1.length()][j] = word2.length() - j;
}
// bottom up dp
for(int i = dp.length - 2;i >= 0;i--){
for(int j = dp[0].length - 2;j >= 0;j--){
if(word1.charAt(i) == word2.charAt(j)){
dp[i][j] = dp[i+1][j+1]; // i.e. no operation needed
}
else{
dp[i][j] = 1 + Math.min(dp[i+1][j], Math.min(dp[i][j+1], dp[i+1][j+1]));
}
}
}
return dp[0][0];
}
}