题目地址:

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. isapi_rewrite运行在.net framework 4.0+iis 6.0环境下404错误解决方案

    今天以前的同事让我帮他上服务器看看,他把页面伪静态之后,出现404错误,为什么会出现这样的问题呢,仔细研究才发现,原因如下: 因为ASP.NET4.0在安装的过程中,已经在IIS6做了一些手脚,让它可 ...

  2. ovirt node的安装简介

    Ovirt安装模式  支持install,update,downupdate,reinstall四种安装方式.  install:全新安装(以前未安装过ovirt node).  update:安装比 ...

  3. 介绍一个C++奇巧淫技

    你能实现这样一个函数吗:   MyType type;   HisType htype;   serialize_3(11, type, htype);   serialize_4(type, hty ...

  4. Lua 解释器

    Lua 解释器 警告⚠️:这将是一个又臭又长的系列教程,教程结束的时候,你将拥有一个除了性能差劲.扩展性差.标准库不完善之外,其他方面都和官方相差无几的 Lua 语言解释器.说白了,这个系列的教程实现 ...

  5. XML实例文档

    from: http://www.w3school.com.cn/xpath/xpath_examples.asp XML实例文档 我们将在下面的例子中使用这个 XML 文档: "books ...

  6. 斯坦福ML公开课笔记15—隐含语义索引、神秘值分解、独立成分分析

    斯坦福ML公开课笔记15 我们在上一篇笔记中讲到了PCA(主成分分析). PCA是一种直接的降维方法.通过求解特征值与特征向量,并选取特征值较大的一些特征向量来达到降维的效果. 本文继续PCA的话题, ...

  7. 解决SQL查询总是超时已过期

    解决SQL查询总是超时已过期 .在WIN8里提示:OLE DB 或 ODBC 错误 : 查询超时已过期; HYT00 1.由于数据库设计问题造成SQL数据库新增数据时超时 症状:   Microso ...

  8. CF 519E(树上倍增求lca)

    传送门:A and B and Lecture Rooms 题意:给定一棵树,每次询问到达点u,v距离相等的点有多少个. 分析:按情况考虑: 1.abs(deep[u]-deep[v])%2==1时, ...

  9. 联系我们_鲲鹏Web数据抓取 - 专业Web数据采集服务提供者

    联系我们_鲲鹏Web数据抓取 - 专业Web数据采集服务提供者 首页 > 联系我们 我们的联系方式如下: 029 - 82542052(陕西 西安) 13389148466 或 13571845 ...

  10. 绝杀600元以下智能手机的夏新小V二代-专栏-速途网

    绝杀600元以下智能手机的夏新小V二代-专栏-速途网 绝杀600元以下智能手机的夏新小V二代