begineerFriendly | java | c++
\nFirstly go and read the code this is further explaination to my code you will get the explaination better after reading the code once\nand this explaination i
**\nFirstly go and read the code this is further explaination to my code you will get the explaination better after reading the code once\nand this explaination is also covering the aswer to the comment .\n\ni m just checking at every index whether that character is equal or not, if equal I just have to move further  , but in case characters are not equal we can have 3 operations, ins, del, replace. and we are storing the minimum out of these three in the DP array.\n\nlet\'s say s1 as "abc" and s2 as "abd" ,and we are at index 2, always if found both characters are different \n\n// Insertion (dp[i][j-1])\nI m always inserting at the right of the current char\n\ns1 - "abc" s2 - "abd"\ni is at c in s1 \nj is at d in s2.\n\nafter insertion\ns1 become "abcd", s2 remain the same "abd" ,\ni is currently at c in s1 \nand j  is currently at d in s2 , but as we have inserted a \'d\' in s1 we have to decrement j , i remains the same \ni = I;    j = j-1;\n\n// deletion   ( dp[i-1][j] )\n\n if I chooses to delete c , instead of inserting d  , in this case if I m deleting c then I have to decrement i , but j remains the same.\ns1 - "abc" s2 - "abd"\n          i is at c in s1\n\t\t  j is at d in s2\n\t\t  \nafter deletion s1 - "ab" s2 - "abd"\n          as we have deleted c from s1 we should decrement i , but we will only decrement j once we found character equal to d in s1\n         I = i-1 , j = j;\n\n// replace (dp[i-1][j-1])\n\nif I choose to replace i m making the current characters equal then I need to decrement both indices , I, j , because current characters are equal \nnow we only have to check for remaining string\n\nex ----- s1 - "abc" s2 - "abd"\nif I choose to replace  \'c\'  by  \'d\'   then both the strings become\ns1 ---- "abd" s2 ---- "abd"\n              as i is at d in s1 \n\t\t\t  and also j is at d in s2 , and both the characters are equal we just have to decrement both.\n   I = i-1 , j = j-1;\t\n   \n   \n\t\nNote we are decrementing j only when both the character matches**\n\t\n\t//\tif character matches then just decrement both the index , but if they don\'t matches then store the minimum possibl;e value by applying\n\t\t// each operation at that particular index.\n\t\t\n\t\tJava code\n\t\t\t\n\t\t\tclass Solution {\n\t\t\t\tpublic int minDistance(String s1, String s2) {\n\t\t\t\t\tint n = s1.length();\n\t\t\t\t\tint m = s2.length();\n\t\t\t\t\tint[][] dp = new int[n+1][m+1];\n\t\t\t\t\t\n\t\t\t\t\t// base cases will contain the length of string which is not empty\n\t\t\t\t\t//because to convert empty string to another string we need no of operations = length of string\n\t\t\t\t\tfor(int i=0;i<=n;i++){\n\t\t\t\t\t\tdp[i][0] = i;\n\t\t\t\t\t}\n\t\t\t\t\tfor(int i=0;i<=m;i++){\n\t\t\t\t\t\tdp[0][i] = i;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int i=1;i<=n;i++){\n\t\t\t\t\t\tfor(int j=1;j<=m;j++){\n\t\t\t\t\t\t\tif(s1.charAt(i-1) == s2.charAt(j-1)){\n\t\t\t\t\t\t\t//as characters are the same we are just decreasing the index\n\t\t\t\t\t\t\t\tdp[i][j] = dp[i-1][j-1];\n\t\t\t\t\t\t\t}else{\n\t\t\t\t\t\t\t//chars are not the same now we have to put the minimum ans out of ans obtained by  insertion,deletion,replace  \n\t\t\t\t\t\t\t\t//\tminimum answer              min(insertion , deletion )          min(deletion,replace)             \n\t\t\t\t\t\t\t\tdp[i][j] = 1 + Math.min( Math.min(dp[i][j-1] ,dp[i-1][j]) , Math.min(dp[i-1][j] ,dp[i-1][j-1]) );\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\treturn dp[n][m];\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\t\t\n\t\t\t//If the post was helpful please upvote , any query can be asked in the comments\n\t\t\t//thankyou for your upvote\n\t\t\t\n\t\t\t\nC++ Code --------- Logic is same in both only difference is in syntax............\n\n\t//\tif character matches then just decrement both the index , but if they don\'t matches then store the minimum possibl;e value by applying\n\t\t// each operation at that particular index.\n\n\t\tclass Solution {\n\t\tpublic:\n\t\t\tint minDistance(string s1, string s2) {\n\t\t\t\tint n = s1.length();\n\t\t\t\tint m = s2.length();\n\t\t\t\tint dp[n+1][m+1];\n\n\t\t\t\t\t// base cases will contain the length of string which is not empty\n\t\t\t\t\t//because to convert empty string to another string we need no of operations = length of string\n\t\t\t\t\t\n\t\t\t\tfor(int i=0;i<=n;i++){\n\t\t\t\t\tdp[i][0] = i;\n\t\t\t\t}\n\t\t\t\tfor(int i=0;i<=m;i++){\n\t\t\t\t\tdp[0][i] = i;\n\t\t\t\t}\n\t\t\t\tfor(int i=1;i<=n;i++){\n\t\t\t\t\tfor(int j =1;j<=m;j++){\n\t\t\t\t\t//as characters are the same we are just decreasing the index\n\t\t\t\t\t\tif(s1[i-1] == s2[j-1]){\n\t\t\t\t\t\t\tdp[i][j] = dp[i-1][j-1];\n\t\t\t\t\t\t}else{\n\t//chars are not the same now we have to put the minimum ans out of ans obtained by  insertion,deletion,replace \n\t//\tminimum answer    =          min(  min(insertion , deletion )  ,  min(deletion,replace)   )          \n\t\t\t\t\t\t\tdp[i][j] = 1+ min(min(dp[i][j-1] , dp[i-1][j]) , min(dp[i-1][j-1] , dp[i-1][j-1]));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn dp[n][m];\n\n\t\t\t}\n\t\t};\n\t\t\t\n\t\t\t\n\t\t\t//If the post was helpful please upvote , any query can be asked in the comments\n\t\t\t//thankyou for your upvote\n\t\t\t\n\t\t\t\n\t\t\t
['Dynamic Programming', 'C', 'Java']