leetcode - Interleaving String
来源:互联网 发布:台式电脑网络连接红叉 编辑:程序博客网 时间:2024/06/11 05:23
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.
//利用dp解决,dp[i][j]的状态表示为s1[0...i] + s2[0...j]的字符串区间能否组成s3.//那么,动态转移方程为:// 1) s1[i-1] == s3[i+j-1] && dp[i-1][j] = true 那么,dp[i][j] = true;// 2) s2[j-1] == s3[i+j-1] && dp[i][j-1] = true 那么,dp[i][j] = true;class Solution {public: bool isInterleave(std::string s1, std::string s2, std::string s3) {if(s1.size() + s2.size() != s3.size()) return false;std::vector<std::vector<bool>> dp(s1.size()+1,std::vector<bool>(s2.size()+1,0));dp[0][0] = 1;for (int i = 1; i < s1.size()+1; i++){if(s1[i-1] == s3[i-1] && dp[i-1][0]) dp[i][0] = true;}for (int i = 1; i < s2.size()+1; i++){if(s2[i-1] == s3[i-1] && dp[0][i-1]) dp[0][i] = true;}for (int i = 1; i < s1.size() + 1; i++){for (int j = 1; j < s2.size() + 1; j++){if(s1[i-1] == s3[i+j-1] && dp[i-1][j]) dp[i][j] = true;if(s2[j-1] == s3[i+j-1] && dp[i][j-1]) dp[i][j] = true;}}return dp[s1.size()][s2.size()]; }};
0 0
- LeetCode: Interleaving String
- LeetCode Interleaving String
- LeetCode: Interleaving String
- [Leetcode] Interleaving String
- [LeetCode] Interleaving String
- leetcode 68: Interleaving String
- [Leetcode] Interleaving String
- LeetCode - Interleaving String
- [LeetCode]Interleaving String
- LeetCode: Interleaving String
- Leetcode: Interleaving String
- [Leetcode]Interleaving String
- Leetcode: Interleaving String
- LeetCode:Interleaving String
- LeetCode-Interleaving String
- [leetcode] Interleaving String@DP
- LeetCode:Interleaving String
- LeetCode - Interleaving String
- 在vs中CUDA下c++混编实现c++运行gpu程序
- mini2440_NFS 利用uboot启动
- 手机浏览器用css设置图片自适应大小
- C++_命名空间
- cin.getline()与getline()
- leetcode - Interleaving String
- 数字电路设计之GoldSchmidt除法的verilog实现
- 轻松搞定面试中的二叉树题目
- PHP正则表达式
- 11. 坐标轴容器盛水问题
- JAVA使用 java-unrar-0.3.jar 解压rar,并且解决中文乱码
- Purpose of Instance Methods vs. Class Methods in Objective-C
- 真理在大炮的射程以内-乌克兰的今天就是中国的昨天
- MapReduce TopK 文件