Skip to main content



Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.

You have the following 3 operations permitted on a word:

  1. Insert a character
  2. Delete a character
  3. Replace a character

Example 1:

Input: word1 = "horse", word2 = "ros"
Output: 3
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')
Solution:- class Solution {
    public int minDistance(String word1, String word2) {
            return word2.length();
            return word1.length();
        if(word1.length()==0 && word2.length()==0){
            return 0;
        int[][] result=new int[word1.length()+1][word2.length()+1];
        for(int i=0;i<=word1.length();i++){
          for(int j=0;j<=word2.length();j++){
        for(int i=1;i<=word1.length();i++){
           // System.out.println(" row "+i);
         for(int j=1;j<=word2.length();j++){
                 //   System.out.println("true");
                 int min=Math.min(result[i-1][j],result[i][j-1]);
                // System.out.println("result[i][j] "+result[i][j]);
        return result[word1.length()][word2.length()];



Popular posts from this blog

Calculating size of User and Cache storage

user storage:- it can be solved using same scenario as mentioned in first article . Suppose there are 12 Million of users are adding every year meaning 1 Million per month. So if we consider 5 year there will be around 60 million of users. Now in terms of data we can look to our DB table and get information about it. A user will be generally having name , id , address , some forien keys . Lets assume 10 columns with each column on an average storing 4 byte of data. 10*4 = 40 bytes of data for one user. 60 * 10^6 * 40 = 2400 * 10^6 = 2.4 * 10^9 = 2.4 GB of data we need to store only user values. Also while calculating storage for the user there is also one important point we need to remember is of ids. If we are going to have 60 Million of users which means 60*10^6 users so unique id’s will be as we know below figures are almost equal

Best LeetCode Lists for Interviews

Here is a list of some of the best questions asked in interviews:-  Must do 75 Must do 60 Must do medium:- Must do Easy:- Graph:-  Dynamic Programming:-  FaceBook interviews:-  Amazon Interviews:-  Google Interviews:-