Problem
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
Example 1:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
Example 2:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false
Solution
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 = 1; i <= l1; i++) dp[i][0] = dp[i-1][0] && s1.charAt(i-1) == s3.charAt(i-1);
for (int j = 1; j <= l2; j++) dp[0][j] = dp[0][j-1] && s2.charAt(j-1) == s3.charAt(j-1);
for (int i = 1; i <= l1; i++) {
for (int j = 1; j <= l2; j++) {
dp[i][j] = dp[i-1][j] && s1.charAt(i-1) == s3.charAt(i+j-1) ||
dp[i][j-1] && s2.charAt(j-1) == s3.charAt(i+j-1);
}
}
return dp[l1][l2];
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。