File tree Expand file tree Collapse file tree 1 file changed +39
-1
lines changed Expand file tree Collapse file tree 1 file changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -31,7 +31,45 @@ public static void main(String[] args) throws Exception {
31
31
32
32
void solve () throws Exception {
33
33
//longest_increasing_subsequence();
34
- longest_common_subsequence ();
34
+ //longest_common_subsequence();
35
+ edit_distance ();
36
+ }
37
+
38
+ void edit_distance (){
39
+ String str1 = "unday" ;
40
+ String str2 = "monday" ;
41
+
42
+ pout .println (edit_distance_dp (str1 ,str2 ));
43
+ }
44
+
45
+ long edit_distance_dp (String s1 ,String s2 ){
46
+ int len1 = s1 .length ();
47
+ int len2 = s2 .length ();
48
+
49
+ long dp [][] = new long [len1 +1 ][len2 +1 ];
50
+
51
+ for (int i =0 ;i <=len1 ;i ++){
52
+ for (int j =0 ;j <=len2 ;j ++){
53
+ if (i ==0 ){
54
+ dp [i ][j ] = j ;
55
+ continue ;
56
+ }
57
+
58
+ if (j ==0 ){
59
+ dp [i ][j ] = i ;
60
+ continue ;
61
+ }
62
+
63
+ if (s1 .charAt (i -1 )==s2 .charAt (j -1 )){
64
+ dp [i ][j ] = dp [i -1 ][j -1 ];
65
+ }
66
+ else {
67
+ dp [i ][j ] = 1 + Math .min (Math .min (dp [i -1 ][j ], dp [i ][j -1 ]), dp [i -1 ][j -1 ]);
68
+ }
69
+ }
70
+ }
71
+
72
+ return dp [len1 ][len2 ];
35
73
}
36
74
37
75
private void longest_common_subsequence () {
You can’t perform that action at this time.
0 commit comments