leetcode@ [97] Interleaving Strings
2015-12-01 12:10
309 查看
https://leetcode.com/problems/interleaving-string/
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 =
View Code
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) { if(s1.length() + s2.length() != s3.length()) return false; if(s1.length() == 0 && s2.length() == 0 && s3.length() == 0) return true; vector<vector<bool> > dp(s1.length()+1, vector<bool>(s2.length()+1, false)); for(int i=1;i<=s1.length();++i) { dp[i][0] = (s1.substr(0,i) == s3.substr(0,i))? true: false; } for(int j=1;j<=s2.length();++j) { dp[0][j] = (s2.substr(0,j) == s3.substr(0,j))? true: false; } for(int i=1;i<=s1.length();++i) { for(int j=1;j<=s2.length();++j) { if(dp[i-1][j] && s1[i-1] == s3[i+j-1]) dp[i][j] = true; else if(dp[i][j-1] && s2[j-1] == s3[i+j-1]) dp[i][j] = true; else dp[i][j] = false; } } return dp[s1.length()][s2.length()]; } };
View Code
相关文章推荐
- 20135321余佳源、20135323符运锦----实验三:实时系统的移植
- CF484D 把数组划分成几段,求所有段的最大值-最小值之和的最大值
- xcode xcconfig
- AppleWatch___学习笔记(二)UI布局和UI控件
- Android studio 添加debug keystore
- 未知尺寸图片在固定高度容器中的水平和垂直居中
- 老男孩linux培训某节课前考试试题及答案分享 【转】
- windows7安装java环境+jmeter工具
- kdiff3工具
- 权限之加载权限资源
- \backend\models\core\Config
- FastCgi与PHP-fpm关系
- 《从零开始学php》总结
- Android 调用系统通讯录
- 每天一个linux命令(52):scp命令
- mysql数据库时间戳与正常时间格式之间的转换
- mysql超时异常的问题查询
- Hibernate:常见错误释义
- 设计模式的六大原则
- json语法