Leetcode 97. Interleaving String
来源:互联网 发布:大数据提供价格 编辑:程序博客网 时间:2024/05/16 17:14
题目:
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.
先用递归的方法来考虑,定义一个函数bool rec(string s1, int p1, string s2, int p2, string s3, int p3),其中p1p2p3分别为s1s2s3当前比较的位置。那么,如果p3如果等于s3的长度,那么返回true,如果p1等于s1的长度,那就判断s2以p2开始往后的子串与s3以p3开始往后的子串是否相等即可,p2等于s2的长度时类似。如果s1的p1位置与s3的p3位置相等,而且s2的p2位置与s3的p3位置也相等,那应该选择哪一个作为被选中的数呢?此时看一下后续哪个成立即可。其中一个成立就行,所以返回rec(s1,p1+1,s2,p2,s3,p3+1) || rec(s1,p1,s2,p2+1,s3,p3+1);如果s1的p1位置和s3的p3位置相等但s2的p2位置与s3的p3位置不相等,那么返回rec(s1,p1+1,s2,p2,s3,p3+1);另一种情况类似。如果上面这些情况都没有存在,那么返回false。
代码:
class Solution {public: bool isInterleave(string s1, string s2, string s3) { if (s1.length() + s2.length() != s3.length()) return false; return rec(s1,0,s2,0,s3,0); } bool rec(string s1, int p1, string s2, int p2, string s3, int p3){ if (p3 == s3.length()) return true; if (p1 == s1.length()) return s2.substr(p2) == s3.substr(p3); if (p2 == s2.length()) return s1.substr(p1) == s3.substr(p3); if (s1[p1] == s3[p3] && s2[p2] == s3[p3]) return rec(s1,p1+1,s2,p2,s3,p3+1) || rec(s1,p1,s2,p2+1,s3,p3+1); else if (s1[p1] == s3[p3]) return rec(s1,p1+1,s2,p2,s3,p3+1); else if (s2[p2] == s3[p3]) return rec(s1,p1,s2,p2+1,s3,p3+1); else return false; }};
想法不错,但一提交,发现Time Limit Exceeded
当然了,递归这种暴力的方法出现超时很正常,那么我们选择一个优雅一点的方法,动态规划
如上图所示,构造一个(m+1)*(n+1)的矩阵,其中m为s1的长度,n为s2的长度,矩阵中位置上的值path[i][j]表示s1取前i位,s2取前j位,是否能组成s3的前i+j位。显然[0][0]处应该为1,那其他位置什么时候置为True呢?首先第一个条件,新添加的字符,要等于s3里面对应的位( i + j 位),第二个条件,之前那个格子也要等于True。举个简单的例子s1 = ab, s2 = c, s3 = bbc ,假设s1已经取了2位,c还没取,此时是False(ab!=bb),我们取s2的新的一位c,即便和s3中的c相等,但是之前是False,所以这一位也是False。同理,如果s1 = ab, s2 = c, s3=abc ,同样的假设,s1取了2位,c还没取,此时是True(ab==ab),我们取s2的新的一位c,和s3中的c相等,且之前这一位就是True,此时我们可以放心置True (abc==abc)
具体代码如下:
C++实现:
class Solution {public: bool isInterleave(string s1, string s2, string s3) { int m = s1.size(); int n = s2.size(); if(m+n != s3.size()) return false; vector<vector<bool> > path(m+1, vector<bool>(n+1, false)); for(int i = 0; i < m+1; i ++) { for(int j = 0; j < n+1; j ++) { if(i == 0 && j == 0) path[i][j] = true; else if(i == 0) path[i][j] = path[i][j-1] && (s2[j-1]==s3[j-1]); else if(j == 0) path[i][j] = path[i-1][j] && (s1[i-1]==s3[i-1]); else path[i][j] = (path[i][j-1] && (s2[j-1]==s3[i+j-1])) || (path[i-1][j] && (s1[i-1]==s3[i+j-1])); } } return path[m][n]; }};
- LeetCode 97. Interleaving String
- [LeetCode]97.Interleaving String
- [Leetcode] 97. Interleaving String
- LeetCode --- 97. Interleaving String
- [leetcode] 97.Interleaving String
- [leetcode] 97.Interleaving String
- leetcode 97. Interleaving String
- Leetcode - 97.Interleaving String
- leetcode 97. Interleaving String
- LeetCode 97. Interleaving String
- LeetCode 97. Interleaving String
- Leetcode:97. Interleaving String
- Leetcode 97. Interleaving String
- [leetcode] 97. Interleaving String
- LeetCode-97.Interleaving String
- [LeetCode] 97. Interleaving String
- leetcode 97.Interleaving String
- 97. Interleaving String ,leetcode
- GDOI模拟4.19~4.21总结
- 省赛D Let's Chat
- 绝对值排序 HDU
- 设置mybatis配置文件xml自动提示
- 设计模式--单例模式python实现
- Leetcode 97. Interleaving String
- 懒虫小鑫
- Mysql学习笔记四——group分组与统计函数
- CF
- hdu 1029 B
- JSP笔记<一>
- 【mfc】常用的代码【更新】
- SD卡刷机笔记
- android存储各种格式文件到sd卡