class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
int l1=s1.length(),l2=s2.length(),l3=s3.length();
if(l1+l2!=l3) return false;
boolean[][] dp=new boolean[l1+1][l2+1];
dp[0][0]=true;
for(int i=0;i<=l1;i++){
for(int j=0;j<=l2;j++){
if(i>0){
dp[i][j]=dp[i][j] || (dp[i-1][j] && s3.charAt(i+j-1)==s1.charAt(i-1));
}
if(j>0){
dp[i][j]=dp[i][j] || (dp[i][j-1] && s3.charAt(i+j-1)==s2.charAt(j-1));
}
}
}
return dp[l1][l2];
}
}
leetcode【中等】97、交错字符串
最新推荐文章于 2024-07-17 19:22:33 发布