Java DP recursive approach

public class Solution {
    public int isInterleave(String s1, String s2, String s3) {
        if(s1.length()+s2.length()!=s3.length())return 0;
        if(s2.length()==0)return s1.equals(s3)?1:0;
        if(s1.length()==0)return s2.equals(s3)?1:0;
        int dp[][]=new int[s1.length()+1][s2.length()+1];
        for(int i=0;i<=s1.length();++i)
			Arrays.fill(dp[i],-1);
        return inter(s1.toCharArray(),s2.toCharArray(),0,0,s3.toCharArray(),0,dp);
    }
    static int inter(char[] s1,char[] s2,int idx1,int idx2,char[] s3,int idx3,int [][]dp){
        if(idx3==s3.length) return 1;
        if(idx1==s1.length&&idx2==s2.length) return 0;
        if(idx1<s1.length&&idx2<s2.length&&s1[idx1]!=s3[idx3]&&s2[idx2]!=s3[idx3]) return 0;
        if(dp[idx1][idx2]!=-1) return dp[idx1][idx2];
        if(idx1==s1.length){
            if(s2[idx2]!=s3[idx3])return 0;
            dp[idx1][idx2]=inter(s1,s2,idx1,idx2+1,s3,idx3+1,dp);
        }
        else if(idx2==s2.length){
            if(s1[idx1]!=s3[idx3])return 0;
            dp[idx1][idx2]=inter(s1,s2,idx1+1,idx2,s3,idx3+1,dp);
        }
        else if(s1[idx1]!=s3[idx3]) dp[idx1][idx2]=inter(s1,s2,idx1,idx2+1,s3,idx3+1,dp);
        else if(s2[idx2]!=s3[idx3]) dp[idx1][idx2]=inter(s1,s2,idx1+1,idx2,s3,idx3+1,dp);
        else dp[idx1][idx2]=Math.max(inter(s1,s2,idx1+1,idx2,s3,idx3+1,dp),inter(s1,s2,idx1,idx2+1,s3,idx3+1,dp));
        return dp[idx1][idx2];
    }
}
Click here to start solving coding interview questions