public int minDistance(String word1, String word2) {
        if (word1 == null || word2 == null) {
            return 0;
        }
        
        String str1, str2;
        if (word1.length() < word2.length()) {
            str1 = word1; str2 = word2; 
        }
        else {
            str1 = word2; str2 = word1;
        }
        
        int index = 0, max = 0;
        for (int k = 0; k < str1.length(); k++) {
            int match = 0;
            for (int i = k; i < str1.length(); i++) {
                char c = str1.charAt(i);
                while (index < str2.length() && str2.charAt(index) != c) {
                    index++;
                }
                if (index == str2.length()) {
                    break;
                }
                else {
                    match++;
                    index++;
                }
            }
            max = Math.max(max, match);
        }
        return str2.length() - max;
    }