Problem: 2050E - Three Strings
Language: java

Solution 1 (runtime: 374 ms):
import java.util.*;

public class Solution {
    static int dp[][];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        while(t-->0){
            String a=sc.next();
            String b=sc.next();
            String c=sc.next();
            dp=new int[a.length()+1][b.length()+1];

            for(int i=0;i<dp.length;i++){
                Arrays.fill(dp[i],-1);
            }
            System.out.println(solve(a,b,c,0,0,0));

        }
    }
    public static int solve(String a,String b,String c,int i,int j,int k){
        if(i==a.length() || j==b.length()){
            return calc(a,b,c,i,j,k);
        }
        if(dp[i][j]!=-1) return dp[i][j];
        if(c.charAt(k)==a.charAt(i) && c.charAt(k)==b.charAt(j)){
            return dp[i][j]=Math.min(solve(a,b,c,i+1,j,k+1),solve(a,b,c,i,j+1,k+1));

        }
        else if(c.charAt(k)==a.charAt(i) ){
            return dp[i][j]=Math.min(solve(a,b,c,i+1,j,k+1),1+solve(a,b,c,i,j+1,k+1));
        }
        else if(c.charAt(k)==b.charAt(j)){
            return dp[i][j]=Math.min(solve(a,b,c,i,j+1,k+1),1+solve(a,b,c,i+1,j,k+1));
        }
        else return dp[i][j]=1+Math.min(solve(a,b,c,i+1,j,k+1),solve(a,b,c,i,j+1,k+1));
    }

    public static int calc(String a,String b,String c,int i,int j,int k){
        int count=0;
        if(i==a.length()){
            while(j<b.length()){
                if(b.charAt(j)!=c.charAt(k)){
                    count++;
                }
                k++;
                j++;
            }

        }
        else{
            while(i<a.length()){
                if(a.charAt(i)!=c.charAt(k)){
                    count++;
                }
                i++;
                k++;
            }
        }
        return count;
    }
}

Solution 2 (runtime: 327 ms):
import java.util.Scanner;



public class Main{

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int t = scanner.nextInt();

        scanner.nextLine(); // Consume the newline



        for (int i = 0; i < t; i++) {

            String a = scanner.nextLine();

            String b = scanner.nextLine();

            String c = scanner.nextLine();



            System.out.println(minChanges(a, b, c));

        }

    }



    private static int minChanges(String a, String b, String c) {

        int lenA = a.length();

        int lenB = b.length();

        int lenC = c.length();



        int[][] dp = new int[lenA + 1][lenB + 1];

        for (int i = 0; i <= lenA; i++) {

            for (int j = 0; j <= lenB; j++) {

                dp[i][j] = Integer.MAX_VALUE;

            }

        }

        dp[0][0] = 0;



        for (int i = 0; i <= lenA; i++) {

            for (int j = 0; j <= lenB; j++) {

                if (i > 0) {

                    dp[i][j] = Math.min(dp[i][j], dp[i - 1][j] + (a.charAt(i - 1) != c.charAt(i + j - 1) ? 1 : 0));

                }

                if (j > 0) {

                    dp[i][j] = Math.min(dp[i][j], dp[i][j - 1] + (b.charAt(j - 1) != c.charAt(i + j - 1) ? 1 : 0));

                }

            }

        }



        return dp[lenA][lenB];

    }

}





// a9JHz5LCXExByemFvIG