@@ -37,34 +37,35 @@ public int minDistance(String word1, String word2) {
37
37
return table [m ][n ];
38
38
}
39
39
}
40
- public static class Solution2 {
40
+
41
+ public static class Solution2 {
41
42
public int minDistance (String word1 , String word2 ) {
42
43
// using levenshtein Distance to find minimum transformation operations required from word 1 to word 2
43
44
int [][] dp = new int [word1 .length ()][word2 .length ()];
44
45
// fill the dp array with -1 value
45
- for (int [] rows : dp ){
46
+ for (int [] rows : dp ) {
46
47
Arrays .fill (rows , -1 );
47
48
}
48
- return levenshteinDistance (word1 , word1 .length ()- 1 , word2 , word2 .length ()- 1 , dp );
49
+ return levenshteinDistance (word1 , word1 .length () - 1 , word2 , word2 .length () - 1 , dp );
49
50
}
50
- private int levenshteinDistance ( String s1 , int s1Index , String s2 , int s2Index , int [][] dp ){
51
51
52
- if (s1Index < 0 ){ // when s1 is "" perform all insertions to get s1 to s2
52
+ private int levenshteinDistance (String s1 , int s1Index , String s2 , int s2Index , int [][] dp ) {
53
+
54
+ if (s1Index < 0 ) { // when s1 is "" perform all insertions to get s1 to s2
53
55
return s2Index + 1 ;
54
- }
55
- else if (s2Index < 0 ) { // when s2 is "" perform all deletions from s1
56
+ } else if (s2Index < 0 ) { // when s2 is "" perform all deletions from s1
56
57
return s1Index + 1 ;
57
58
}
58
59
59
60
// base condition when dp array is filled, return the distance
60
- if (dp [s1Index ][s2Index ] != -1 )
61
+ if (dp [s1Index ][s2Index ] != -1 ) {
61
62
return dp [s1Index ][s2Index ];
63
+ }
62
64
63
- if (s1 .charAt (s1Index ) == s2 .charAt (s2Index )){
65
+ if (s1 .charAt (s1Index ) == s2 .charAt (s2Index )) {
64
66
// Characters match, no edit distance to be calculated
65
67
dp [s1Index ][s2Index ] = levenshteinDistance (s1 , s1Index - 1 , s2 , s2Index - 1 , dp );
66
- }
67
- else {
68
+ } else {
68
69
// When there is a character mismatch, perform operations
69
70
70
71
// Insertion
0 commit comments