More Readable N*N space solution


  • 0
    C
    public class Solution {
        public int minDistance(String word1, String word2) {
            int len1 = word1.length();
            int len2 = word2.length();
            int[][] map = new int[len1+1][len2+1];
            
            for (int i=0; i<=len1; i++) {
                map[i][0] = i;
            }
            
            for (int j=0; j<=len2; j++) {
                map[0][j] = j;
            }
            
            for (int i=0; i<len1; i++) {
                for (int j=0; j<len2; j++) {
                    if (word1.charAt(i) == word2.charAt(j)) {
                        map[i+1][j+1] = map[i][j];
                    } else {
                        map[i+1][j+1] = Math.min(map[i][j+1], map[i+1][j]) + 1;
                    }
                }
            }
            
            return map[len1][len2];
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.