https://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.

这题有点类似于之前的求编辑距离的题:http://www.cnblogs.com/biyeymyhjob/archive/2012/09/28/2707343.html

定义f[i][j]为 以s1[0,i]s2[0,j]匹配s3[i][j]

当j==0时,f[i][0]= f[i-1][0] && s1[i-1]==s3[i-1]

当i==0时,f[0][j]= f[0][j-1] && s2[j-1]==s3[j-1]

当i>=1&&j>=1时,f[i][j]= (f[i-1][j] && s1[i-1]==s3[i-1][j]) || (f[i][j-1] && s2[j-1]== s3[i][j-1])

bool isInterleave(string s1, string s2, string s3) {
if(s3.length()!=s1.length()+s2.length())
return false; vector<vector<bool>> f(s1.length()+,vector<bool>(s2.length()+,true));
for(int i=;i<=s1.length();i++)
f[i][]= f[i-][] && s1[i-]==s3[i-]; for(int i=;i<=s2.length();i++)
f[][i]=f[][i-] && s2[i-]==s3[i-]; for(int i=;i<=s1.length();i++)
{
for(int j=;j<=s2.length();j++)
{
f[i][j]=(f[i-][j] && s1[i-]==s3[i+j-]) || (f[i][j-] && s2[j-]==s3[i+j-]);
}
} return f[s1.length()][s2.length()];
}

Interleaving String的更多相关文章

  1. [LeetCode] Interleaving String - 交织的字符串

    题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...

  2. 40. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  3. 【leetcode】Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  4. 二维动态规划——Interleaving String

    97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2 ...

  5. 【一天一道LeetCode】#97. Interleaving String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...

  6. LeetCode之“动态规划”:Interleaving String

    题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...

  7. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

  8. 【LeetCode】97. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  9. [LeetCode] Interleaving String 交织相错的字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...

随机推荐

  1. 单例模式中用volatile和synchronized来满足双重检查锁机制

    背景:我们在实现单例模式的时候往往会忽略掉多线程的情况,就是写的代码在单线程的情况下是没问题的,但是一碰到多个线程的时候,由于代码没写好,就会引发很多问题,而且这些问题都是很隐蔽和很难排查的. 例子1 ...

  2. 2016第三届C++大会参会感悟(上)

    继05年第一届C++大会,09年第二届,2016年10月28日-29日,在上海举行第三届C++大会.讲师主要有C++之父 / Bjarne Stroustrup,前Facebook研究科学家 / An ...

  3. [HTML5] ArrayBuffer与类型化数组

    写在前面 这是关于JS二进制操作的第三篇博客,前两篇详见: [HTML5] Blob对象 [HTML5] FileReader对象 此前从宏观角度介绍了如何通过JS创建一个二进制对象,并介绍了如何将本 ...

  4. OleDbDataReader快速数据读取方式

    查询得到OleDbDataReader后,有三种方式支持数据读取,如下: //方法一**速度中等 OleDbDataReader reader = command.ExecuteReader(); w ...

  5. WebForm业务系列-会员功能

    用了这么久的webform,还记得刚开始根本不知道程序要写成什么样,只知道功能实现了就行,有很多现实的问题都没考虑到.所以程序改了又改,最后连自己做的什么都不知道了.所以,现在来总结一下. 会员功能 ...

  6. GStreamer 记录

    GStreamer 是一个新的多媒体框架,大大简化了多媒体工具的开发流程,比如,这里有一个 IBM 的文档,介绍了一个 MP3 播放器. http://www.ibm.com/developerwor ...

  7. dataTables添加序号和行选中框

    <table id="mytable" class="table table-striped table-bordered" width="10 ...

  8. 2016第七季极客大挑战Writeup

    第一次接触CTF,只会做杂项和一点点Web题--因为时间比较仓促,写的比较简略.以后再写下工具使用什么的. 纯新手,啥都不会.处于瑟瑟发抖的状态. 一.MISC 1.签到题 直接填入题目所给的SYC{ ...

  9. Table 表单样式

    <style> table th { white-space: nowrap; background-color: #f5f5f5; height:30px; font-size:14px ...

  10. vector it->和*it

    //每次写代码总是被迭代器的iter->和*iter弄晕,主要是被protobuf弄晕了 #include <vector> struct test{ test(){ memset( ...