给出三个字符串:s1、s2、s3,推断s3是否由s1和s2交叉构成。

您在真实的面试中是否遇到过这个题?

Yes
例子

比方 s1 = "aabcc" s2 = "dbbca"

- 当 s3 = "aadbbcbcac",返回  true.

- 当 s3 = "aadbbbaccc", 返回 false.

挑战

要求时间复杂度为O(n^2)或者更好

标签 Expand



相关题目 Expand


分析:两个字符串的问题,大部分都能够用dp[i][j]表示第一个字符串前i个字符第二个字符串前j个字符的匹配情况来解决
代码:
class Solution {
public:
/**
* Determine whether s3 is formed by interleaving of s1 and s2.
* @param s1, s2, s3: As description.
* @return: true of false.
*/
bool isInterleave(string s1, string s2, string s3) {
// write your code here
if(s3.length()!=s1.length()+s2.length())
return false;
if(s1.length()==0)
return s2==s3;
if(s2.length()==0)
return s1==s3;
vector<vector<bool> > dp(s1.length()+1,vector<bool>(s2.length()+1,false));
dp[0][0] = true;
for(int i=1;i<=s1.length();i++)
dp[i][0] = dp[i-1][0]&&(s3[i-1]==s1[i-1]);
for(int i=1;i<=s2.length();i++)
dp[0][i] = dp[0][i-1]&&(s3[i-1]==s2[i-1]);
for(int i=1;i<=s1.length();i++)
{
for(int j=1;j<=s2.length();j++)
{
int t = i+j;
if(s1[i-1]==s3[t-1])
dp[i][j] = dp[i][j]||dp[i-1][j];
if(s2[j-1]==s3[t-1])
dp[i][j] = dp[i][j]||dp[i][j-1];
}
}
return dp[s1.length()][s2.length()];
}
};

LintCode-交叉字符串的更多相关文章

  1. LintCode——交叉字符串

    描述:给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例:s1 = "aabcc" s2 = "dbbca" - 当 s3 = &quo ...

  2. lintcode 中等题:interleaving String 交叉字符串

    题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...

  3. 交叉字符串 · Interleaving String

    [抄题]: 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成.(洗牌) 比如 s1 = "aabcc" s2 = "dbbca" - 当 s3 ...

  4. lintcode :同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  5. lintcode :旋转字符串

    题目: 旋转字符串 给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转) 样例 对于字符串 "abcdefg". offset=0 => "abcdef ...

  6. Lintcode--006(交叉字符串)

    Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Ex ...

  7. LintCode翻转字符串问题 - python实现

    题目描述:试实现一个函数reverseWords,该函数传入参数是一个字符串,返回值是单词间做逆序调整后的字符串(只做单词顺序的调整即可). 例如:传入参数为"the sky is blue ...

  8. LintCode——旋转字符串

    描述:给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转) 样例:对于字符串 "abcdefg"     offset=0 => "abcdefg&qu ...

  9. [LintCode]转换字符串到整数

    问题描述: 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-21 ...

  10. Interleaving String,交叉字符串,动态规划

    问题描述: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Give ...

随机推荐

  1. 普通androidproject转换为C/C++project之后,再还原成androidproject的解决方式

    我们在调试android程序时,可能会把androidproject转换成C/C++project,或者Add Native Support.可是,我们怎么把C/C++project还原成普通的and ...

  2. Spring-SpringJdbcTemlate配置介绍

    使用spring的jdbcTemplate进一步操作JDBC 一.普通配置  SpringJdbcTemplate连接数据库并操作数据 1.applicationContext.xml 1.1 建立D ...

  3. yarn架构——本质上是在做解耦 将资源分配和应用程序状态监控两个功能职责分离为RM和AM

    Hadoop YARN架构解读 原Mapreduce架构 原理架构图如下: 图 1.Hadoop 原 MapReduce 架构 原 MapReduce 程序的流程:首先用户程序 (JobClient) ...

  4. js接收文件流并下载

    js接收文件流并下载 标签(空格分隔): js 在此输入正文 <script type="text/javascript"> function download(fil ...

  5. Django迁移到mysql数据库时的错误

    pip install mysqlclient Collecting mysqlclient Using cached https://files.pythonhosted.org/packages/ ...

  6. 在 Ubuntu 15.04 上安装 Android Studio(极其简单)

    sudo apt-add-repository ppa:paolorotolo/android-studio sudo apt-get update sudo apt-get install andr ...

  7. 关于ubuntu中文输入调用不出来的解决办法,具体如正文。

    卸载了 fcitx sudo apt-get remove fcitx 重启 sudo reboot 重新安装 fcitxsudo apt-get install fcitx 安装拼音输入法sudo ...

  8. 多元一次方程解法 C++

    #include<iostream> #include<math.h> #include<fstream> #include<stdlib.h> usi ...

  9. 高并发之web服务器负载均衡简单介绍

    负载均衡种类 F5,七层负载均衡,四层负载均衡 Nginx负载均衡 内置策略.扩展策略 内置策略:IPHash.加权轮询 扩展策略:fair策略.通用hash.一致性hash 加权轮询策略 首先将请求 ...

  10. 想写一个 Sketch 插件 结果 一查不可收拾 ~~ 涉及到 Symbol 符号/ Layer 图层 / Overrides 可替换变量 等等

    var sketch = context.api() var document = sketch.selectedDocument; var selection = document.selected ...