97. Interleaving String(js)
97. Interleaving String
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
题意:s3是否与s1和s2字符相交错相等
代码如下:
/** * @param {string} s1 * @param {string} s2 * @param {string} s3 * @return {boolean} */ var isInterleave = function(s1, s2, s3) { if(s3.length!==s1.length+s2.length){ return false; } var res=[]; for(var i=0;i<s1.length+1;i++){ res[i]=new Array(); for(var j=0;j<s2.length+1;j++){ res[i][j]=false; } } for(var i=0;i<s1.length+1;i++){ for(var j=0;j<s2.length+1;j++){ if(i===0 && j===0){ res[i][j]=true; }else if(i===0){ res[i][j]=(res[i][j-1] && s2[j-1]===s3[i+j-1]) }else if(j===0){ res[i][j]=(res[i-1][j] && s1[i-1]===s3[i+j-1]) }else{ res[i][j]=(res[i-1][j] && s1[i-1]===s3[i+j-1]) || (res[i][j-1] && s2[j-1]===s3[i+j-1]) } } } return res[s1.length][s2.length] };