题目

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组合而成。(每个字符串中的字母相对顺序不可变)
开始看到题目,没有解决思路。参考网友的答案,发现解决题目的两种思路。
方法一:递归的思路,但是该方法对于大集合数据会出现TLE
方法二:动态规划的思路
根据字符串1和2,建立判定二维矩阵。

AC代码

class Solution {
public:
/*方法一:递归实现,对大数据组会TLE*/
bool isInterleave1(string s1, string s2, string s3) {
int len1 = s1.length(), len2 = s2.length(), len3 = s3.length(); if (len2 == 0)
return s1 == s3;
else if (len1 == 0)
return s2 == s3;
else if (len3 == 0)
return len1 + len2 == 0;
else
{
if (s1[0] == s3[0] && s2[0] != s3[0])
return isInterleave1(s1.substr(1), s2, s3.substr(1));
else if (s1[0] != s3[0] && s2[0] == s3[0])
return isInterleave1(s1, s2.substr(1), s3.substr(1));
else if (s1[0] == s3[0] && s2[0] == s3[0])
return isInterleave1(s1.substr(1), s2, s3.substr(1)) || isInterleave(s1, s2.substr(1), s3.substr(1));
else
return false;
}//else
} /*方法二:二维动态规划*/
bool isInterleave(string s1, string s2, string s3) {
int len1 = s1.length(), len2 = s2.length(), len3 = s3.length();
if (len1 + len2 != len3)
return false;
else if (len2 == 0)
return s1 == s3;
else if (len1 == 0)
return s2 == s3;
else if (len3 == 0)
return len1 + len2 == 0;
else
{
vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1, 0));
dp[0][0] = 1;
for (int i = 1; i <= len1; ++i)
{
if (s1[i - 1] == s3[i - 1])
dp[i][0] = 1;
else
break;
}//for for (int j = 1; j <= len2; ++j)
{
if (s2[j - 1] == s3[j - 1])
dp[0][j] = 1;
else
break;
}//for for (int i = 1; i <= len1; ++i)
{
for (int j = 1; j <= len2; ++j)
{
if (s1[i - 1] == s3[i + j - 1])
dp[i][j] = dp[i - 1][j] || dp[i][j];
if (s2[j - 1] == s3[i + j - 1])
dp[i][j] = dp[i][j - 1] || dp[i][j];
}//for
}//for
return dp[len1][len2] == 1;
}//else
}
};

  

GitHub测试程序源码

LeetCode(97) Interleaving String的更多相关文章

  1. LeetCode(97):交错字符串

    Hard! 题目描述: 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = &qu ...

  2. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  3. char型字符串(数组)与string型字符串 指针与引用

    一.常指针: int *const p;    //指针不可改变,但是指针指向的数据可以改变. 指向常量的指针: const int *p;    //指针可以改变,但是指针指向的数据不可以改变. 指 ...

  4. JAVA基础学习之路(九)[2]String类常用方法

    字符与字符串: 1.将字符数组变为字符串(构造方法) public String(char[] value) Allocates a new String so that it represents ...

  5. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  6. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  7. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  8. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  9. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

随机推荐

  1. 日志处理之logging模块

    日志级别: 'CRITICAL': CRITICAL, 'ERROR': ERROR, 'WARN': WARNING, 'WARNING': WARNING, 'INFO': INFO, 'DEBU ...

  2. [Altera]PLL仿真

    EDA Tools: 1.Quartus II 13.1(64-bit) 2.Modelsim SE-64 10.1c Time: 2016.05.05 ----------------------- ...

  3. 对象化前端表单(Form)提交

    很常见的业务场景,就是前端一个表单,submit给后台,在web.form时代,有from 的runat="server" 配合submit 自动会提交给服务端,然后服务端解析Re ...

  4. 需要交互的shell编程——EOF(转载)

    在shell编程中,”EOF“通常与”<<“结合使用,“<<EOF“表示后续的输入作为子命令或子shell的输入,直到遇到”EOF“, 再次返回到主调shell,可将其理解为分 ...

  5. ajaxfileupload.js的简单使用

    上传文件 未选择任何文件 引入 <script src="../javaScript/ajaxfileupload.js"></script> <bu ...

  6. android 对View的延时更换内容

    一.当ImageView按下时可以跟换一张按下效果的图片进行显示,使用postDelayed即可以让view在规定时间后执行run()中的内容 img.setImageResource(R.drawa ...

  7. StartFP

    1.INODS执行完成时间为13:06:04分, 从日志信息无法知道STARTFP执行到哪一步 从INODS执行完成时间可知道startFp执行时间为13:06:05分开始, 执行StartFP中的e ...

  8. 阿里巴巴Java招聘

    大家好: 我是阿里巴巴B2B的应用架构师,现在大量招聘Java工程师,对自己技术有信心的兄弟姐妹,请联系我吧. 版权声明:本文为博主原创文章,未经博主允许不得转载.

  9. 9×9扫雷游戏代码-C写的

    #include <stdio.h> #include <stdlib.h> //画棋盘 a雷表 b周围雷数表 c打开表 ][],][],][]) { ,j=; ;i<; ...

  10. Windows 程序设计

    一.Win32 API /******************************************************************** created: 2014/04/1 ...