public int minDistance2(String word1, String word2) {
        int m = word1.length();
        int n = word2.length();

        int[] memoUp = new int[n + 1];
        int[] memoDown = new int[n + 1];

        for(int i = 0; i < n + 1; i++){
            memoUp[i] = i;
        }

        int j = 1;
        while(j <= m){
            for(int i = 0; i < n + 1 && j <= m; i++){
                if(i == 0){
                    memoDown[i] = j;
                }else{
                    if(word1.charAt(j - 1) == word2.charAt(i - 1)){
                        memoDown[i] = memoUp[i - 1];
                    }else{
                        memoDown[i] = Math.min(Math.min(memoUp[i], memoDown[i - 1]), memoUp[i - 1]) + 1;
                    }
                }
            }
            int[] tmp = memoUp;
            memoUp = memoDown;
            memoDown = tmp;
            j++;
        }
        return memoUp[n];
    }