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

public class Q1 {
    public static long solve(long n, HashMap<Long, Long> map) {
        if (n <= 3) {
            return 1;
        }

        if (map.containsKey(n)) {
            return map.get(n);
        }

        long count = solve(n / 4, map) + solve(n / 4, map);
        map.put(n, count);
        return map.get(n);
    }

    public static long gcd(long a, long b) {
        if (a == 0)
            return b;
        if (b == 0)
            return a;

        // Base case
        if (a == b)
            return a;

        // a is greater
        if (a > b) {
            if (a % b == 0)
                return b;
            return gcd(a - b, b);
        }
        if (b % a == 0)
            return a;
        return gcd(a, b - a);
    }

    public static int solve(int ai, int ci, String a, String b, String c, int dp[][]) {
        int bi = ci - ai;
        if (ai == a.length()) {
            String s1 = b.substring(bi);
            String s2 = c.substring(ci);
            if (s1.equals(s2)) {
                return 0;
            }
            int cnt = 0;
            for (int i = 0; i < s1.length(); i++) {
                if (s1.charAt(i) != s2.charAt(i)) {
                    cnt++;
                }
            }

            return cnt;
        }

        if (bi == b.length()) {
            String s1 = a.substring(ai);
            String s2 = c.substring(ci);
            if (s1.equals(s2)) {
                return 0;
            }
            int cnt = 0;
            for (int i = 0; i < s1.length(); i++) {
                if (s1.charAt(i) != s2.charAt(i)) {
                    cnt++;
                }
            }

            return cnt;
        }

        if (dp[ai][ci] != -1) {
            return dp[ai][ci];
        }

        int min = Integer.MAX_VALUE;
        if (a.charAt(ai) == c.charAt(ci)) {
            min = Math.min(solve(ai + 1, ci + 1, a, b, c, dp), min);
        }

        if (b.charAt(bi) == c.charAt(ci)) {
            min = Math.min(solve(ai, ci + 1, a, b, c, dp), min);
        }

        if (a.charAt(ai) != c.charAt(ci) || b.charAt(bi) != c.charAt(ci)) {
            min = Math.min(1 + Math.min(solve(ai + 1, ci + 1, a, b, c, dp), solve(ai, ci + 1, a, b, c, dp)), min);
        }

        return dp[ai][ci] = min;
    }

    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();
            int dp[][] = new int[a.length()][c.length()];
            for (int row[] : dp) {
                Arrays.fill(row, -1);
            }
            System.out.println(solve(0, 0, a, b, c, dp));
        }
    }
}