-
Notifications
You must be signed in to change notification settings - Fork 26
/
583. Delete Operation for Two Strings
46 lines (42 loc) · 1.43 KB
/
583. Delete Operation for Two Strings
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
class Solution {
public int minDistance(String word1, String word2) {
int length = helper(word1,word2);
return word1.length() + word2.length() - 2 * length ;
}
public int lcs(String text1, String text2) {
int[][] dp = new int[text1.length()+1][text2.length()+1];
for(int i=0;i<=text1.length();i++){
for(int j=0;j<=text2.length();j++){
if(i==0 || j==0){
continue;
}
if(text1.charAt(i-1)==text2.charAt(j-1)){
dp[i][j] = 1 + dp[i-1][j-1];
}
else{
dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
}
}
}
return dp[text1.length()][text2.length()];
}
}
class Solution {
public int minDistance(String text1, String text2) {
int[][] dp = new int[text1.length()+1][text2.length()+1];
for(int i=0;i<=text1.length();i++){
for(int j=0;j<=text2.length();j++){
if(i==0 || j==0){
dp[i][j] = i+j;
}
else if(text1.charAt(i-1)==text2.charAt(j-1)){
dp[i][j] = dp[i-1][j-1];
}
else{
dp[i][j] = 1 + Math.min(dp[i-1][j],dp[i][j-1]);
}
}
}
return dp[text1.length()][text2.length()];
}
}