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: 280 ms):
import java.util.*;

public class ThreeStrings {
    // Custom print functions
    public static void print(Object print) {
        System.out.print(print);
    }

    public static void println(Object print) {
        System.out.println(print);
    }

    public static void println() {
        System.out.println();
    }

    // Declare a 2D dp array to store memoization results
    static int[][] dp;

    // Function to compute the minimum changes required
    public static int func(int i, int j, char[] a, char[] b, char[] c) {
        int n = c.length;
        // Base case: if all characters are matched
        if (i + j >= n) return 0;

        // Check if result is already calculated
        if (dp[i][j] != -1) return dp[i][j];

        int take_a = Integer.MAX_VALUE;
        int take_b = Integer.MAX_VALUE;

        // Try taking a character from 'a'
        if (i < a.length) {
            if (a[i] == c[i + j]) {
                take_a = func(i + 1, j, a, b, c); // No change needed
            } else {
                take_a = 1 + func(i + 1, j, a, b, c); // Change needed
            }
        }

        // Try taking a character from 'b'
        if (j < b.length) {
            if (b[j] == c[i + j]) {
                take_b = func(i, j + 1, a, b, c); // No change needed
            } else {
                take_b = 1 + func(i, j + 1, a, b, c); // Change needed
            }
        }

        // Store and return the minimum changes required
        return dp[i][j] = Math.min(take_a, take_b);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt(); // Number of test cases

        for (int l = 0; l < t; l++) {
            char[] a = sc.next().toCharArray(); // Input string a
            char[] b = sc.next().toCharArray(); // Input string b
            char[] c = sc.next().toCharArray(); // Target string c

            int m = a.length;
            int n = b.length;
            int len = c.length;

            // Initialize dp array with -1
            dp = new int[m + 1][n + 1];
            for (int[] row : dp) {
                Arrays.fill(row, -1);
            }

            // Compute and print result
            println(func(0, 0, a, b, c));
        }
        sc.close();
    }
}