Problem: 2050E - Three Strings
Language: java

Solution 1 (runtime: 233 ms):
import java.util.Scanner;

public class Main {
    private static final int MAX_LEN = 2003; // Ensure larger array size for safety

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        scanner.nextLine(); // Consume the newline character

        while (t-- > 0) {
            String a = scanner.nextLine();
            String b = scanner.nextLine();
            String c = scanner.nextLine();

            int n = a.length();
            int m = b.length();
            int[][] dp = new int[n + 1][m + 1]; // Use n + 1 and m + 1 for proper indexing

            for (int i = 0; i <= n; i++) {
                for (int j = 0; j <= m; j++) {
                    dp[i][j] = MAX_LEN; // Initialize with a large value
                }
            }

            dp[0][0] = 0;

            for (int i = 0; i <= n; i++) {
                for (int j = 0; j <= m; 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));
                    }
                }
            }

            System.out.println(dp[n][m]);
        }
        scanner.close(); // Close the scanner to avoid resource leaks
    }
}


Solution 2 (runtime: 156 ms):
import java.util.*;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;

public class E_Three{
    public static void main(String[] args) throws IOException{
        InputReader sc= new InputReader (System.in);
        int t=sc.readInt();
        while(t-->0){
            
          String a=sc.readString();
          String b=sc.readString();
          String c=sc.readString();
          int max=(int)1e9;
          int n=a.length();
          int m=b.length();
          int[][] dp=new int[n+1][m+1];
          for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                dp[i][j]=max;
            }
          }
          dp[0][0]=0;
          for(int i=0;i<n;i++){
            dp[i+1][0]=dp[i][0]+(a.charAt(i)==c.charAt(i)?0:1);
          }
          for(int j=0;j<m;j++){
            dp[0][j+1]=dp[0][j]+(b.charAt(j)==c.charAt(j)?0:1);
          }
          for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                dp[i][j]=Math.min(dp[i-1][j]+(a.charAt(i-1)==c.charAt(i+j-1)?0:1),dp[i][j-1]+(b.charAt(j-1)==c.charAt(i+j-1)?0:1));
            }
          }
          System.out.println(dp[n][m]);



          
            

           
            
            



            
            
            
            

            
            
            
           
            
           
           

            

           


           
           
        


    }

}
public static int countOccurence(String s1,String s2){
    int n=s1.length();
    int m=s2.length();
    int [][] dp=new int[n+1][m+1];
    for(int i=0;i<=m;i++){
        dp[0][i]=0;
    }
    for(int i=0;i<=n;i++){
        dp[i][0]=1;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(s1.charAt(i-1)==s2.charAt(j-1)){
                dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
            }
            else{
                dp[i][j]=dp[i-1][j];
            }
        }
    }
    return dp[n][m];
}
public static int solve(int[] a,int x){
    int [] count=new int[a.length+1];
    int n=count.length;
    for(int i:a){
        if(i<count.length){
            count[i]++;
        }
    }
    int result=0;
    while(count[result]!=0){
        if(result+x<n){
            count[result+x]+=count[result]-1;
        }
        result++;
    }
    return result;
}

public static BigInteger factorial (int n){
    BigInteger result=BigInteger.ONE;
    for(int i=1;i<=n;i++){
        result=result.multiply(BigInteger.valueOf(i));
    }
    return result;
}
public static int [] count(int[] p,int n,String s){
    boolean [] visited=new boolean[n+1];
    int [] zeroCount=new int[n+1];
    for(int i=1;i<=n;i++){
        if(!visited[i]){
            List<Integer> cycle=new ArrayList<>();
            int curr=i;
            while(!visited[curr]){
                visited[curr]=true;
                cycle.add(curr);
                curr=p[curr];
            }
            int count=0;
            for(int ind:cycle){
                if(s.charAt(ind-1)=='0'){
                    count++;
                }
            }
            for(int ind:cycle){
                zeroCount[ind]=count;
            }
        }
        
    }
    return zeroCount;
}
public static int gcd(int a,int b){
    if(b==0){
        return a;
    }
    return gcd(b,a%b);
}
public static boolean isPrime(int n){
    boolean isPrime=true;
    for(int i=2;i*i<=n;i++){
        if(n%i==0){
            isPrime=false;
            break;
        }
    }
    return isPrime;
}
   
static final class InputReader {
        private final InputStream stream;
        private final byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
 
        public InputReader(InputStream stream) {
            this.stream = stream;
        }
 
        private int read() throws IOException {
            if (curChar >= numChars) {
                curChar = 0;
                numChars = stream.read(buf);
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }
 
        public final int readInt() throws IOException {
            return (int)readLong();
        }
 
        public final long readLong() throws IOException {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
                if (c == -1) throw new IOException();
            }
            boolean negative = false;
            if (c == '-') {
                negative = true;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return negative ? -res : res;
        }
 
        public final String readString() throws IOException {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.append((char)c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }
public final char readChar() throws IOException {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            return (char) c;
        }
 
        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
    }
 
}