题目

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. C#中有哪些类型的数组

    一维数组(Single-Dimensional)多维数组(Multidimensional)交错数组(Jagged arrays):交错数组是元素为数组的数组.交错数组元素的维度和大小可以不同.交错数 ...

  2. MySQL追踪优化器小试

    首先看一下MySQL追踪优化器的典型用法: 打开:SET optimizer_trace="enabled=on"; 查询优化器的信息:SELECT * FROM INFORMAT ...

  3. SSRS动态设置文本框属性

    SSRS可以通过表达式动态设置文本框所有的属性,比如字体,字号,是否加粗,如下图所示: 汉字和数字英文字母占用的空间不一样,一个汉字占用两个数字和英文字母的空间,VB里有LENB取得字节数,这SSRS ...

  4. jsp与数据库的连接

    经过一段时间的学习与上网查资料,我已经成功的用java语言连接上了数据库, 本以为同理jsp跟数据库的连接肯定水到渠成的,但是在经过尝试很多次后我发现现实永远是骨感的,最终结果是花了一个下午的时间去建 ...

  5. textview 弹出键盘上面添加完成按钮,并设置输入内容的格式。

    - (void)setContentView{ self.contentTextView = [[UITextView alloc]initWithFrame:CGRectMake(11, 70, S ...

  6. Samba 共享文件后在Windows 上无法访问的问题

    /etc/samba/smb.conf的配置如下: #============================ Share Definitions ========================== ...

  7. mysql分表和表分区详解

    为什么要分表和分区? 日常开发中我们经常会遇到大表的情况,所谓的大表是指存储了百万级乃至千万级条记录的表.这样的表过于庞大,导致数据库在查询和插入的时候耗时太长,性能低下,如果涉及联合查询的情况,性能 ...

  8. 把cmd信息中的正常和异常输出分别输出到不同txt文件中

    场景一: 1.大量滚动信息容纳不下,在小黑屏中被冲刷掉. 2.希望把正常输出和异常输出分别输出到不同地方. 相关命令 一共有4个输出到文件的命令,现以jar命令打war包举例说明: 命令 说明 举例  ...

  9. js对象的继承以及公有私有属性的定义和读写

    最近想写一些js工具,有些方面需要用到面向对象的方法,比如继承父类属性和方法.通过私有化隐藏某些对象的属性等,因为没有系统的学习js,所以不知道怎么做,觉得很伤脑筋. 今天受到技术群里朋友的提示,并查 ...

  10. python初学day01

    1.执行Python脚本时打印的字符有颜色 1. print "\033[32;1mhello\033[0m" #打印绿色 2. print "\033[31;1mhel ...