题目

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. java -cp

    java -cp /home/hdp/log4jTest/log4j-1.2.17.jar:/home/hdp/log4jTest/testLog.jar:/home/hdp/log4jTest/co ...

  2. Python全栈开发day5

    一.lambda表达式 对于简单的函数,存在一种简便的表示方式,即:lambda表达式 1 2 3 >>> shaw = lambda x,y:x + y >>> ...

  3. android SurfaceView中播放视频 按视频的原始比例播放

    OnPreparedListener mediaPlayerOnPreparedListener = new OnPreparedListener() { @Override public void ...

  4. awk的涂鸦

    awk太牛了,博大精深,学不透,学了不用,又忘. 所以花了一天,自己总结了基础,以后就当字典查(容易忘).有不对的地方,忘大家指出. [ganzl@cmdb ~]$ more /etc/passwd ...

  5. 修改C# 新建类模板

    找到安装路径下的这个文件夹:D:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache\CSh ...

  6. RedHat6安装gcc

    RedHat RHEL 6.0安装gcc的方法 最近在折腾RedHat6.0 ,全是命令,号称比windows安全的Linux,没有界面,也不知道所谓的专家们如何比的,一个界面做的非常好的Window ...

  7. Rethrowing exceptions and preserving the full call stack trace

    refer:http://weblogs.asp.net/fmarguerie/archive/2008/01/02/rethrowing-exceptions-and-preserving-the- ...

  8. 团队编程——web应用之人事管理系统

    本次作业为团队作业,团队博客要求如下:1. 介绍团队情况:包括队长.成员.队名.成员照片.队训--.等:2. 介绍团队项目名称.总体任务,各成员任务等:3. 每个队做 一次需求调研(针对团队项目),要 ...

  9. Ajax1

    一.Ajax是什么? 全称"Asynchronous JavaScript and XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJA ...

  10. Eclipse设置默认注释

    在 windows-->preferenceJava-->Code Style-->Code Templatescode-->new Java file点编辑,覆盖原文本: $ ...