【原创】leetCodeOj --- Interleaving String 解题报告
题目地址:
https://oj.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 = "aabcc",
s2 = "dbbca",
When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.
方法:
第一时间能想到的方法就是类似归并排序的思想。维护3个下标,分别指向s1、s2和s3。若s1[index1] == s3[index3]则 index1 ++ && index3 ++。反之若s2[index2] == s3[index3]则 index2 ++ && index3 ++。
但是这有个问题,就是如果s2[index2] == s1[index1]的话,选择谁呢?
答案是谁都选一次看看。于是,我们又要动态规划了。。
设dp[m][n] 为s1从下标m开始,s2从下标n开始是否能匹配下标从m + n开始的s3 (为什么是m + n?因为s1 m下标以前的字符和s2 n下标以前的字符都已经匹配过了)。如果值为1,那么可以匹配;如果为2,那么不能匹配。如果为0,那么需要递归调用子问题来解决。
简单看看动态规划方程:
dp[m][n] = {
s1[m] == s2[n] == s3[m+n] : dp[m][n+1] || dp[m+1][n]; // 若s1和s2当前都可以匹配,就都试一下,谁行谁就上。短路
s1[m] == s3[m+n] : dp[m+1][n]; // 仅当s1可以匹配
s2[n] == s3[m+n] : dp[m][n+1]; // 仅当s2可以匹配
else : false; // 谁都无法匹配
}
全部代码:
class Solution {
public:
string t1,t2,t3;
char dp[][]; // 没处理过就是0,处理了可以就是1,不行就是2
bool isInterleave(string s1, string s2, string s3) {
int len1 = s1.size();
int len2 = s2.size();
int len3 = s3.size();
if (len1 + len2 != len3)
return false;
if (len1 == )
return s2 == s3 ? true : false;
if (len2 == )
return s1 == s3 ? true : false;
t1 = s1;
t2 = s2;
t3 = s3;
return trueStuff(,,len1,len2);
}
bool trueStuff(int m,int n,int len1,int len2) // m is index for s1,n is index for s2.
{
if (m >= len1 && n >= len2)
return true;
if (dp[m][n] != )
return dp[m][n] == ? true : false;
bool tmp = false; // mark if dp[m][n] can do the right thing.
if (m >= len1)
tmp = (t2[n] == t3[m+n] ? trueStuff(m,n + ,len1,len2) : false);
else if (n >= len2)
tmp = (t1[m] == t3[m+n] ? trueStuff(m + ,n,len1,len2) : false);
else if (t1[m] == t2[n] && t1[m] == t3[m+n])
tmp = (trueStuff(m + ,n,len1,len2) || trueStuff(m,n + ,len1,len2));
else if (t1[m] == t3[m+n])
tmp = trueStuff(m + ,n,len1,len2);
else if (t2[n] == t3[m+n])
tmp = trueStuff(m,n + ,len1,len2);
else
tmp = false;
dp[m][n] = (tmp == true ? : );
return tmp;
}
};
【原创】leetCodeOj --- Interleaving String 解题报告的更多相关文章
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【原创】leetCodeOj --- Largest Number 解题报告
原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...
- 【原创】leetCodeOj --- Min Stack 解题报告
题目地址: https://oj.leetcode.com/problems/min-stack/ 题目内容: Design a stack that supports push, pop, top, ...
- 【原创】leetCodeOj --- Dungeon Game 解题报告
原题地址: https://oj.leetcode.com/problems/dungeon-game/ 题目内容: The demons had captured the princess (P) ...
- 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)
题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...
- 【原创】leetCodeOj --- Sort List 解题报告
今日leetcode链表题全制霸 原题地址: https://oj.leetcode.com/problems/sort-list/ 题目内容: Sort List Sort a linked lis ...
随机推荐
- Github干货系列:C++资源集合-
Awesome CPP,这又是一个 Awesome XXX 系列的资源整理,由 fffaraz 发起和维护.内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. ...
- Thinkphp框架拓展包使用方式详细介绍--验证码实例(十一)
原文:Thinkphp框架拓展包使用方式详细介绍--验证码实例(十一) 拓展压缩包的使用方式详细介绍 1:将拓展包解压:ThinkPHP3.1.2_Extend.zip --> 将其下的 \ ...
- Enthought科学计算,数据分析
Enthought Canopy: Easy Python Deployment Plus Integrated Analysis Environment for Scientific Computi ...
- STL 源代码分析 算法 stl_algo.h -- includes
本文senlie原,转载请保留此地址:http://blog.csdn.net/zhengsenlie includes(应用于有序区间) ------------------------------ ...
- Java生成目录
Java生成目录 1.说明 推断目录是否存在,假设不存在就创建该目录.并打印其路径.假设存在,打印其路径 2.实现源代码 /** * @Title:BuildFolder.java * @Packag ...
- 上下文菜单与TrackPopupMenu
这算是一个演示程序吧,想不到上下文菜单也是採用ON_COMMAND宏来进行消息映射,在这里,我发现一个问题:从CWnd派生的类ON_UPDATE_COMMAND_UI_RANGE似乎没有效果,不知道应 ...
- PowerDesigner中SQL文件、数据库表反向生成PDM
1 反向生成PDM 1) 创建一个空的PDM模型(选择相应的DBMS): 2) 选择[Database]--[Update Model from Database ...
- hdu 2451 Simple Addition Expression(数位DP )成败在于细节
亚洲区域赛的题,简单的数位DP题,注重细节. 任何细节都有可能导致wa,所以没有绝对的水题. 把握好细节,此题便A. #include<stdio.h> __int64 getans(__ ...
- 孙鑫HTML视频学习总结
1. HTML中元素和标签 元素是由单个或一对标签定义的包含范围.一个标签就是左右分别有一个小于号(<)和大于号(>)的字符串.开始标签是指以不以斜杠(/)开头的标签,其内是一串允许的 ...
- H3C TE老版本OSPF正确配置
R1配置: ---------------------------------------------------- # sysname RT1# super password level 3 cip ...