@@ -37,34 +37,35 @@ public int minDistance(String word1, String word2) {
3737return table [m ][n ];
3838 }
3939 }
40- public static class Solution2 {
40+
41+ public static class Solution2 {
4142public int minDistance (String word1 ,String word2 ) {
4243// using levenshtein Distance to find minimum transformation operations required from word 1 to word 2
4344int [][]dp =new int [word1 .length ()][word2 .length ()];
4445// fill the dp array with -1 value
45- for (int []rows :dp ){
46+ for (int []rows :dp ) {
4647Arrays .fill (rows , -1 );
4748 }
48- return levenshteinDistance (word1 ,word1 .length ()- 1 ,word2 ,word2 .length ()- 1 ,dp );
49+ return levenshteinDistance (word1 ,word1 .length () - 1 ,word2 ,word2 .length () - 1 ,dp );
4950 }
50- private int levenshteinDistance (String s1 ,int s1Index ,String s2 ,int s2Index ,int [][]dp ){
5151
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
5355return 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
5657return s1Index +1 ;
5758 }
5859
5960// base condition when dp array is filled, return the distance
60- if (dp [s1Index ][s2Index ] != -1 )
61+ if (dp [s1Index ][s2Index ] != -1 ) {
6162return dp [s1Index ][s2Index ];
63+ }
6264
63- if (s1 .charAt (s1Index ) ==s2 .charAt (s2Index )){
65+ if (s1 .charAt (s1Index ) ==s2 .charAt (s2Index )) {
6466// Characters match, no edit distance to be calculated
6567dp [s1Index ][s2Index ] =levenshteinDistance (s1 ,s1Index -1 ,s2 ,s2Index -1 ,dp );
66- }
67- else {
68+ }else {
6869// When there is a character mismatch, perform operations
6970
7071// Insertion