LintCode-交叉字符串
来源:互联网 发布:手机淘宝链接打不开 编辑:程序博客网 时间:2024/05/16 10:03
给出三个字符串:s1、s2、s3,判断s3是否由s1和s2交叉构成。
Yes
样例
比如 s1 = "aabcc" s2 = "dbbca"
- 当 s3 = "aadbbcbcac",返回 true.
- 当 s3 = "aadbbbaccc", 返回 false.
挑战
标签 Expand 要求时间复杂度为O(n^2)或者更好
相关题目 Expand
分析:两个字符串的问题,大部分都可以用dp[i][j]表示第一个字符串前i个字符第二个字符串前j个字符的匹配情况来解决
代码:
class Solution {public: /** * Determine whether s3 is formed by interleaving of s1 and s2. * @param s1, s2, s3: As description. * @return: true of false. */ bool isInterleave(string s1, string s2, string s3) { // write your code here if(s3.length()!=s1.length()+s2.length()) return false; if(s1.length()==0) return s2==s3; if(s2.length()==0) return s1==s3; vector<vector<bool> > dp(s1.length()+1,vector<bool>(s2.length()+1,false)); dp[0][0] = true; for(int i=1;i<=s1.length();i++) dp[i][0] = dp[i-1][0]&&(s3[i-1]==s1[i-1]); for(int i=1;i<=s2.length();i++) dp[0][i] = dp[0][i-1]&&(s3[i-1]==s2[i-1]); for(int i=1;i<=s1.length();i++) { for(int j=1;j<=s2.length();j++) { int t = i+j; if(s1[i-1]==s3[t-1]) dp[i][j] = dp[i][j]||dp[i-1][j]; if(s2[j-1]==s3[t-1]) dp[i][j] = dp[i][j]||dp[i][j-1]; } } return dp[s1.length()][s2.length()]; }};
0 0
- LintCode-交叉字符串
- LintCode(M)交叉字符串
- LintCode 29-交叉字符串
- LintCode 29 交叉字符串
- LintCode 交叉字符串
- lintcode -- 交叉字符串
- LintCode 交叉字符串
- lintcode 29. 交叉字符串
- lintcode interleaving-string 交叉字符串
- [Lintcode] Interleaving String 交叉字符串
- (lintcode)第29题交叉字符串
- lintcode 交叉链表
- 交叉字符串
- 交叉字符串
- 交叉字符串
- 交叉字符串
- 交叉字符串
- 交叉字符串
- pig 问题集锦
- 各种移动GPU压缩纹理的使用方法
- jni浅析
- 人脸性别识别文献阅读笔记(2)
- wamp在服务器win8-64中的配置,使用域名访问
- LintCode-交叉字符串
- [易飞]事务与另一个进程被锁在锁资源上,并且被选做锁牺牲品
- Unable to execute dex: Multiple dex files define Landroid/support/v7/appcompat/R$anim;问题解决
- HDU--5396(区间dp+排列组合)
- Hadoop自动化集群部署脚本
- 指针及地址笔记
- Spring 的ClassPathXmlApplicationContext()路径读取错误
- MFC中CTreeCtrl加载节点缓慢的分析和解决方法
- 深入理解Javascript window对象