Problem:
Edit Distance (Levenshtein Distance)

Find the edit distance between given two words s1 and s2.

The disntace is the minimum number of single-character edits required to change one word into the other. The edits including the following operations:

insertion: Insert a character at a particular position.

deletion: Delete a character at a particular position.

substitution: Change the character at a particular position to a different character

Input

s1
s2

Two words s1 and s2 are given in the first line and the second line respectively. The words will consist of lower case characters.

Output

Print the edit distance in a line.

Constraints

1 ≤ length of s1 ≤ 1000

1 ≤ length of s2 ≤ 1000

Sample Input 1

acac
acm

Sample Output 1

2

Sample Input 2

icpc
icpc

Sample Output 2

0

Solution in JAVA (judge verdict: Wrong Answer)


import java.util.Scanner;

public class Main {
    static int[][] dp;
    static Scanner sc;

//    public static void main(String[] args) {
//        sc = new Scanner(System.in);
//        int n = sc.nextInt();
//        for (int i = 0; i < n; i++) {
//            solve();
//        }
//    }
//
//    static void solve() {
//        char[] a = sc.next().toCharArray();
//        char[] b = sc.next().toCharArray();
//        if (a.length < b.length) {
//            char[] c = a;
//            a = b;
//            b = c;
//        }
//        dp = new int[a.length + 1][b.length + 1];
//        for (int i = 0; i < a.length; i++) {
//            for (int j = 0; j < b.length; j++) {
//                dp[i + 1][j + 1] = a[i] == b[j] ? dp[i][j] + 1 : Math.max(dp[i + 1][j], dp[i][j + 1]);
//            }
//        }
//        System.out.println(dp[a.length][b.length]);
//    }

    public static void main(String[] args) {
        sc = new Scanner(System.in);
        char[] a = sc.next().toCharArray();
        char[] b = sc.next().toCharArray();
        if (a.length < b.length) {
            char[] c = a;
            a = b;
            b = c;
        }
        dp = new int[a.length + 1][b.length + 1];
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < b.length; j++) {
                dp[i + 1][j + 1] = a[i] == b[j] ? dp[i][j] + 1 : Math.max(dp[i + 1][j], dp[i][j + 1]);
            }
        }
        System.out.println(Math.max(a.length, b.length) - dp[a.length][b.length]);

    }

}