[LeetCode] Interleaving String 解题思路
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.
问题 : 给定三个字符串 s1, s2, s3, 求 s3 是不是由 s1 和 s2 交错组合而成。
感觉是一条比较典型的 DP 题目。
设 i, j, k 分别是 s1, s2, s3 待求解的当前下标。
- 当 s3[k] != s1[i] 且 s3[k] != s2[j], 则匹配失败
- 当 s3[k] 只和 s1[i] 相等,则 k++ 和 i++
- 当 s3[k] 只和 s2[j] 相等,则 k++ 和 j++
- 当 s3[k] == s1[i] 且 s3[k] == s2[j] ,则分别求 k++ 和 i++ ,以及 k++ 和 j++ ,两者取或运算
由于 s3 恰好有 s1 和 s2 交错行程,所以 s3 长度必然等于 s1 + s2 的长度,知道其中二者就能确定第三个数,这样只需要二维数组保存中间结果即可。
vector<vector<int>> vv; bool interIleave(string s1, string s2, string s3, int p1, int p2, int p3){ // cout << s1 << "\n" << s2 << "\n" << s3 << "\n--------\n"; if (s1.size() == ) {
return (s2 == s3);
} if (s2.size() == ) {
return (s1 == s3);
} if (vv[p1][p3] != -) {
return vv[p1][p3];
} if (s3[] != s1[] && s3[] != s2[]) {
vv[p1][p3] = false;
return false;
} if (s3[] == s1[] && s3[] != s2[]) {
bool tmpb = interIleave(s1.substr(), s2, s3.substr(), p1+, p2, p3+);
vv[p1][p3] = tmpb;
return tmpb;
} if (s3[] != s1[] && s3[] == s2[]) {
bool tmpb = interIleave(s1, s2.substr(), s3.substr(), p1, p2+, p3+);
vv[p1][p3] = tmpb;
return tmpb;
} bool inter1 = interIleave(s1.substr(), s2, s3.substr(), p1+, p2, p3+);
bool inter2 = interIleave(s1, s2.substr(), s3.substr(), p1, p2+, p3+);
bool res = inter1 || inter2; vv[p1][p3] = res; return res;
} bool isInterleave(string s1, string s2, string s3) { vector<vector<int>> tmp(s1.size(), vector<int>(s3.size(), -));
vv = tmp; if (s3.size() != s1.size()+s2.size()) {
return false;
} if (s3.size() == ) {
return true;
} bool res = interIleave(s1, s2, s3, , , ); return res;
}
[LeetCode] Interleaving String 解题思路的更多相关文章
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- [LeetCode] Word Break 解题思路
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [LeetCode] Interleaving String [30]
题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...
- [leetcode]Interleaving String @ Python
原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is ...
- [LeetCode] Maximum Gap 解题思路
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- [LeetCode] Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- [Leetcode] Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [LeetCode] Distinct Subsequences 解题思路
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- 基于 Redis 数据累计的实现
需求:对上传文件进行统一编号,以 xxx-YYYYmmdd-000001为编码格式,其中YYYYmmdd为当天传的日期. 技术方案:redis,get,set,incr,expireAt即可实现. p ...
- [BUGFIX]__import_pywin32_system_module__
import_pywin32_system_module 修复方法: 编辑 X:/Python27/Lib/site-packages/win32/lib/pywintypes.py 第114行 if ...
- Keil的c语言编译器
我曾经通过查看反汇编代码对KEILC编译器进行了测试,大概有这么一下内容,也得出一些结论. (1)全局变量:如果程序中定义了全局变量,而且初始值不是0.此时,在程序调到main()函数执行前,除了要进 ...
- POJ 3122 Pie 二分枚举
题目:http://poj.org/problem?id=3122 这个题就好多了,没有恶心的精度问题,所以1A了.. #include <stdio.h> #include <ma ...
- Word2003中如何使封面和目录中不插入页码
Word2003中如何使封面和目录中不插入页码?? 转载自: http://blog.zzedu.net.cn/user1/zhaoweijie/archives/2010/187266.html ...
- 推荐一款不错的GIF录制软件附带.NET源码
源码下载地址:http://www.51aspx.com/Code/ScreenToGif10
- delphi xe5 android 手机上使用sqlite
本篇我们介绍一下在android手机上怎样使用sqlite数据库,这里用Navigator实现 增删改查. 1.新建firemonkey mobile application 2.选择blank ap ...
- bootstrap兼容IE8 解决办法
bootstrap有使用CSS3,所以ie9以下浏览器不会很顺畅.IE9以下的兼容性不是很好.IE本身就是怪胎,就搞一些和外界标准不一致.搞得web开发考虑这考虑那的兼容性,蛋疼! 基本上css3的没 ...
- Python的startswith和endswith
做文本处理的时候经常要判断一个文本有没有以一个子串开始,或者结束.Python为此提供了两个函数: S.startswith(prefix[, start[, end]]) -> bool 如果 ...
- bzoj 3144: [Hnoi2013]切糕 最小割
3144: [Hnoi2013]切糕 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 681 Solved: 375[Submit][Status] ...