class Solution {
//用dp[i][j]表示s1的前i个字符和s2的前j个字符能否组成s3的前i+j个字符
public boolean isInterleave(String s1, String s2, String s3) {
int n1 = s1.length();
int n2 = s2.length();
int n3 = s3.length();
if(n1 + n2 != n3){
return false;
}
boolean[][] dp = new boolean[n1 + 1][n2 + 1];
dp[0][0] = true;
for(int i = 0; i <= n1; i++){
for(int j = 0; j <= n2; j++){
//如果s1[i - 1] == s3[i + j - 1]那么dp[i][j]可以由状态dp[i - 1][j] 转移来
//注意dp下标从1开始
if(i - 1 >= 0 && s1.charAt(i - 1) == s3.charAt(i + j - 1)){
dp[i][j] |= dp[i - 1][j];
}
if(j - 1 >= 0 && s2.charAt(j - 1) == s3.charAt(i + j - 1)){
dp[i][j] |= dp[i][j - 1];
}
}
}
return dp[n1][n2];
}
}