Java | C++ | Python3 | with detailed explanation
\nEXPLANATION:-\n\nIn this problem, we need to convert w1 to w2 with minimum number of operations\n\nLet\'s see how to solve this problem.\n\nCase 1:-\n   w1 = 
```\nEXPLANATION:-\n\nIn this problem, we need to convert w1 to w2 with minimum number of operations\n\nLet\'s see how to solve this problem.\n\nCase 1:-\n   w1 = ab,  w2 = ""\n   result = 2.\n   we can convert w1 to w2 by \n   deleting all the characters in w1.\n   So, result is 2.\nCase 2:-\n   w1 = "", w2 = ab\n   we can convert w1 to w2, \n   by inserting a and b it w1.\n   So, result is 2.\nCase 3:-\n   w1 = a,  w2 = a\n   w1 and w2 are equal.\n   So, result is 0.\n   \nCase 4:-\n    w1 = a,  w2 = b\n\tHere, w1 and w2 are not equal.\n\tSo, What to do now.\n\tWe have three operations\n\t1. insert\n\t2. delete\n\t3. replace\n\tperform three operations on w1.\n\tand take minimum result.\n\t\n\tw1 = a,  w2 = b.\n\tindex1 = 0\n\tindex2 = 0\n\toperations = 0\n\t\n\t1.insert \n\t\n\tinsert b to w1.\n\tSo, w1 = ba, w2 = b\n\toperations is increased by since we have inserted a character.\n\toperations = 1\n\tNow, w1[index1] == w2[index2],\n\tSo, ignore those characters and check remaining characters.\n    So, w1 = a, w2 = "" (after ignoring first characters as they are same)\n\tThis is same as case 1.\n\twe can w2 from w1 by deleting \'a\'.\n\tSo, delete \'a\'\n\t- >  operations = 2.\n\n\t 2. Delete:-\n\n\t w1 = a, w2 = b, operations = 0\n\t delete a character from w1\n\t So, operations = 1\n\t w1 = "", w2 = b\n\t this is same as case 2.\n\t we can w2 from w1 by inserting \'b\' to w1.\n\t So, insert \'b\'.\n\t - > operations = 2.\n\n\t 3. replace:\n\t \n\t  w1 = a, w2 = b, operations = 0\n\t  replace a with b.\n\t  operations = 1.\n\t  w1 = w2\n\t  So, operations = 1.\n  \n\t  Now, choose minimum of (insert, delete, replace) = min(2, 2, 1)\n  \n\t  So, result = 1 to convert a to b.\n  \n  This is the idea behind this.\n  \n  IF YOU HAVE ANY DOUBTS, FEEL FREE TO ASK.\n \xA0IF YOU UNDERSTAND, DON\'T FORGET TO UPVOTE.\n```\n```\n\nJava : -\nclass Solution {\n    public int minDistance(String word1, String word2) {\n\t\tint M = word1.length();\n\t\tint N = word2.length();\n\t\tint dp[][] = new int[M + 1][N + 1];\n\t\tif (M * N == 0)\n\t\t\treturn M + N;\n\n\t\tfor (int i = 0; i <= M; i++) {\n\t\t\tdp[i][0] = i;\n\t\t}\n\t\tfor (int i = 0; i <= N; i++) {\n\t\t\tdp[0][i] = i;\n\t\t}\n\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tfor (int j = 1; j <= N; j++) {\n\t\t\t\tif (word1.charAt(i - 1) == word2.charAt(j - 1)) {\n\t\t\t\t\tdp[i][j] = dp[i - 1][j - 1];\n\t\t\t\t} else {\n\t\t\t\t\tdp[i][j] = Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn dp[M][N];\n\n\t}\n}\n\nC++:-\nclass Solution {\npublic:\n    int minDistance(string word1, string word2) {\n\t\tint M = word1.size();\n\t\tint N = word2.size();\n\t\tvector<vector<int>> dp(M + 1, vector<int>(N + 1, 0));\n\t\tif (M * N == 0)\n\t\t\treturn M + N;\n\n\t\tfor (int i = 0; i <= M; i++) {\n\t\t\tdp[i][0] = i;\n\t\t}\n\t\tfor (int i = 0; i <= N; i++) {\n\t\t\tdp[0][i] = i;\n\t\t}\n\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tfor (int j = 1; j <= N; j++) {\n\t\t\t\tif (word1.at(i - 1) == word2.at(j - 1)) {\n\t\t\t\t\tdp[i][j] = dp[i - 1][j - 1];\n\t\t\t\t} else {\n\t\t\t\t\tdp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn dp[M][N];\n    }\n};\n\nPython3:-\nclass Solution:\n    def minDistance(self, word1: str, word2: str) -> int:\n        M, N = len(word1), len(word2)\n        if M * N == 0:\n            return M + N\n        dp = [[0] * (N + 1) for i in range(M + 1)]\n\n        for i in range(M + 1):\n            dp[i][0] = i\n        for i in range(N + 1):\n            dp[0][i] = i\n        for i in range(1, M + 1):\n            for j in range(1, N + 1):\n                if word1[i - 1] == word2[j - 1]:\n                    dp[i][j] = dp[i - 1][j - 1]\n                else:\n                    dp[i][j] = 1 + min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1]))\n        return dp[M][N]\n```
[]