short DP in Java


  • 0
    K
    public class Solution {
        public boolean isInterleave(String s1, String s2, String s3) {
            int len1 = s1.length(), len2 = s2.length(), len3 = s3.length();
            
            boolean dp[][][] = new boolean[len1+1][len2+1][len3+1];
            dp[len1][len2][len3] = true;
        
            for(int i = len1; i >= 0; i--){
                for(int j = len2; j >= 0; j--){
                    for(int k = len3 - 1; k >= 0; k--){
                        dp[i][j][k] |= ((i < len1 && s1.charAt(i) == s3.charAt(k) ? dp[i+1][j][k+1] : false) | 
                                            (j < len2 && s2.charAt(j) == s3.charAt(k) ? dp[i][j+1][k+1] : false));
                    }
                }
            }
            return dp[0][0][0];
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.