题目地址:

https://oj.leetcode.com/problems/interleaving-string/

题目内容:

Given s1s2s3, 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 解题报告的更多相关文章

  1. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

  2. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  3. 【LeetCode】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  4. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  5. 【原创】leetCodeOj --- Largest Number 解题报告

    原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...

  6. 【原创】leetCodeOj --- Min Stack 解题报告

    题目地址: https://oj.leetcode.com/problems/min-stack/ 题目内容: Design a stack that supports push, pop, top, ...

  7. 【原创】leetCodeOj --- Dungeon Game 解题报告

    原题地址: https://oj.leetcode.com/problems/dungeon-game/ 题目内容: The demons had captured the princess (P) ...

  8. 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)

    题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...

  9. 【原创】leetCodeOj --- Sort List 解题报告

    今日leetcode链表题全制霸 原题地址: https://oj.leetcode.com/problems/sort-list/ 题目内容: Sort List Sort a linked lis ...

随机推荐

  1. POJ 3176:Cow Bowling

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13464   Accepted: 8897 Desc ...

  2. C / C++算法学习笔记(7)-双向冒泡

    原始地址:双向冒泡 通常的冒泡是单向的,而这里是双向的,也就是说还要进行反向的工作. 代码看起来复杂,仔细理一下就明白了,是一个来回震荡的方式. 写这段代码的作者认为这样可以在冒泡的基础上减少一些交换 ...

  3. 《转》在win7,boa-constructor 0.6.1 的palette面板中没有控件图标的解决方法

    原地址:http://blog.csdn.net/rickleo/article/details/6532595 在win7-64bit环境下,boa-constructor 0.6.1 的palet ...

  4. Advanced Data Structures

    Advanced Data Structures Advanced Data Structures

  5. HDU/HDOJ 2612 Find a way 双向BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 思路:从两个起点出发,有多个终点,求从两个起点同时能到达的终点具有的最小时间,开两个数组分别保存 ...

  6. OC对象创建过程

    在利用OC开发应用程序中,须要大量创建对象,那么它的过程是什么呢? 比方:NSArray *array = [[NSArrayalloc] init]; 在说明之前,先把OC的Class描写叙述一下: ...

  7. 非常不错的android应用开发详解在安卓开发中

    我们在苹果开发中,总会看到XCode,Interface Builder,Object-c这如此入耳入随的单词,但往往多数人在认为XCODE看着简单,InterfaceBuilder好似操作, 而Ob ...

  8. fedora 18 源码编译 android 4.0.1

    1.编译环境: 系统:fedora 18 KED 桌面  (Fedora-18-i686-Live-KDE.iso) 处理器:酷睿i5双核 内存: 4GB 硬盘:46GB java版本:java ve ...

  9. 巧妙使用Firebug插件,快速监控网站打开缓慢的原因

    原文 巧妙使用Firebug插件,快速监控网站打开缓慢的原因 很多用户会问,我的网站首页才50KB,打开网页用了近60秒才打开?如何解释? 用户抱怨服务器运行缓慢,w3wp.exe 出现 CPU 10 ...

  10. OSG+VS2010+win7环境搭建

    Win7下 osg+vs2010环境搭建 一.相关准备 a) Osg源代码 当前最新版:OpenSceneGraph的3.0.0.zip 下载链接: http://www.openscenegraph ...