首页 > 代码库 > Interleaving String
Interleaving String
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.
class Solution {public: bool isInterleave(string s1, string s2, string s3) { int l1 = s1.length(); int l2 = s2.length(); int l3 = s3.length(); if(l3 != (l1+l2)) return false; bool dp[l1+1][l2+1]; for(int i=0;i<l1+1;i++){ for(int j=0;j<l2+1;j++){ if(i==0 && j==0){ dp[i][j] = true; }else if (i == 0){ dp[i][j] = (dp[i][j-1] && s2[j-1] == s3[i+j-1]); }else if (j == 0){ dp[i][j] = (dp[i-1][j] && s1[i-1] == s3[i+j-1]); }else { dp[i][j] = (dp[i][j-1] && s2[j-1] == s3[i+j-1]) || (dp[i-1][j] && s1[i-1] == s3[i+j-1]); } } } return dp[l1][l2]; }};
Interleaving String
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。