题目来源:leetcode
Given
s1,
s2,
s3, find whether
s3 is formed by the interleaving of
s1 and
s2.
For example,
Given:
s1 ="aabcc",
s2 ="dbbca",
When s3 ="aadbbcbcac", return true.
When s3 ="aadbbbaccc", return false.
思路:DP http://blog.csdn.net/u011095253/article/details/9248073
class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if (s3==null||s1==null||s2==null)
return false;
if((s1.length()+s2.length())!=s3.length())
return false;
boolean dp[][]=new boolean[s1.length()+1][s2.length()+1];
dp[0][0]=true;
for (int i=1;i<=s2.length();i++){
if (dp[0][i-1]==true&&s2.charAt(i-1)==s3.charAt(i-1))
dp[0][i]=true;
}
for (int i=1;i<=s1.length();i++){
if (dp[i-1][0]==true&&s1.charAt(i-1)==s3.charAt(i-1))
dp[i][0]=true;
}
for (int i=1;i<=s1.length();i++)
for (int j=1;j<=s2.length();j++){
if ((dp[i-1][j]==true&&s1.charAt(i-1)==s3.charAt(i+j-1))||(dp[i][j-1]==true&&s2.charAt(j-1)==s3.charAt(i+j-1)))
dp[i][j]=true;
}
return dp[s1.length()][s2.length()];
}
}