97.Interleaving String (二维动态规划,没想明白)
2015-07-01 15:53
375 查看
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
For example,
Given:
s1 =
s2 =
When s3 =
When s3 =
Here is some explanation:
DP table represents if s3 is interleaving at (i+j)th position when s1 is at ith position, and s2 is at jth position. 0th position means empty string.
So if both s1 and s2 is currently empty, s3 is empty too, and it is considered interleaving. If only s1 is empty, then if previous s2 position is interleaving and current s2 position char is equal to s3 current position char, it is considered interleaving.
similar idea applies to when s2 is empty. when both s1 and s2 is not empty, then if we arrive i, j from i-1, j, then if i-1,j is already interleaving and i and current s3 position equal, it s interleaving. If we arrive i,j from i, j-1, then if i, j-1 is already
interleaving and j and current s3 position equal. it is interleaving.
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
if(s3.length() != s1.length() + s2.length())
return false;
bool table[s1.length()+1][s2.length()+1];
for(int i=0; i<s1.length()+1; i++)
for(int j=0; j< s2.length()+1; j++){
if(i==0 && j==0)
table[i][j] = true;
else if(i == 0)
table[i][j] = ( table[i][j-1] && s2[j-1] == s3[i+j-1]);
else if(j == 0)
table[i][j] = ( table[i-1][j] && s1[i-1] == s3[i+j-1]);
else
table[i][j] = (table[i-1][j] && s1[i-1] == s3[i+j-1] ) || (table[i][j-1] && s2[j-1] == s3[i+j-1] );
}
return table[s1.length()][s2.length()];
}
};
For example,
Given:
s1 =
"aabcc",
s2 =
"dbbca",
When s3 =
"aadbbcbcac", return true.
When s3 =
"aadbbbaccc", return false.
Here is some explanation:
DP table represents if s3 is interleaving at (i+j)th position when s1 is at ith position, and s2 is at jth position. 0th position means empty string.
So if both s1 and s2 is currently empty, s3 is empty too, and it is considered interleaving. If only s1 is empty, then if previous s2 position is interleaving and current s2 position char is equal to s3 current position char, it is considered interleaving.
similar idea applies to when s2 is empty. when both s1 and s2 is not empty, then if we arrive i, j from i-1, j, then if i-1,j is already interleaving and i and current s3 position equal, it s interleaving. If we arrive i,j from i, j-1, then if i, j-1 is already
interleaving and j and current s3 position equal. it is interleaving.
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
if(s3.length() != s1.length() + s2.length())
return false;
bool table[s1.length()+1][s2.length()+1];
for(int i=0; i<s1.length()+1; i++)
for(int j=0; j< s2.length()+1; j++){
if(i==0 && j==0)
table[i][j] = true;
else if(i == 0)
table[i][j] = ( table[i][j-1] && s2[j-1] == s3[i+j-1]);
else if(j == 0)
table[i][j] = ( table[i-1][j] && s1[i-1] == s3[i+j-1]);
else
table[i][j] = (table[i-1][j] && s1[i-1] == s3[i+j-1] ) || (table[i][j-1] && s2[j-1] == s3[i+j-1] );
}
return table[s1.length()][s2.length()];
}
};
相关文章推荐
- chapter15test3
- PHP中static关键字以及与self关键字的区别
- GIT学习链接
- Java学习篇之---Mysql中varchar类型总结
- Mysql:This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME 错误解决
- Perl语言学习笔记 9 正则表达式处理文本
- (Win7 x64)NetBeans 8.0.2 使用Tomcat 8作为服务器
- 使用AutoMapper
- Oracle 基础篇 --- 表删除
- 我的自定义Adapter
- Android onTouch事件与手势操作
- DEV-aspxgridview行客户端单击双击事件RowClick、RowDblClick 、
- Oracle查询某个时间段内第天的统计数
- 显示网页当前时间代码六种
- swift实作app 交流分享五(資料傳遞)
- JS选中checkbox后获取table内一行TD所有数据的方法
- hibernate 中的拦截器EmptyInterceptor接口功能
- OS开发拓展篇—音频处理(音乐播放器1)
- PHP中创建图像并绘制文字的例子
- c++ vector