题目:

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

Example

For s1 = "aabcc", s2 = "dbbca"

  • When s3 = "aadbbcbcac", return true.
  • When s3 = "aadbbbaccc", return false.

题解:

Solution 1 ()

class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int n1 = s1.size(), n2 = s2.size(), n3 = s3.size();
if (n1 + n2 != n3) {
return false;
}
vector<vector<int>> dp(n1 + , vector<int>(n2 + , false));
dp[][] = true;
for (int i = ; i <= n1; ++i) {
dp[i][] = dp[i - ][] && (s1[i - ] == s3[i - ]);
}
for (int i = ; i <= n2; ++i) {
dp[][i] = dp[][i - ] && (s2[i - ] == s3[i - ]);
}
for (int i = ; i <= n1; ++i) {
for (int j = ; j <= n2; ++j) {
dp[i][j] = (dp[i - ][j] && s1[i - ] == s3[i - + j]) || (dp[i][j - ] && s2[j - ] == s3[j - + i]);
}
}
return dp[n1][n2];
}
};

Solution  2 ()

class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
if(s1.length() + s2.length() != s3.length())
return false;
bool dp[s2.length() + ];
for(int i = ; i <= s1.length(); i++){
for(int j = ; j <= s2.length(); j++){
if(i == && j == )
dp[j] = true;
else if(i == )
dp[j] = (dp[j - ] && s3[i + j - ] == s2[j - ]);
else if(j == )
dp[j] = (dp[j] && s3[i + j - ] == s1[i - ]);
else
dp[j] = (dp[j] && s3[i + j - ] == s1[i - ]) || (dp[j - ] && s3[i + j - ] == s2[j - ]);
}
}
return dp[s2.length()];
}
};

  DFS

Solution 3 ()

  BFS

Solution 4 ()

【Lintcode】029.Interleaving String的更多相关文章

  1. 【LeetCode】97. Interleaving String

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

  2. 【原创】leetCodeOj --- Interleaving String 解题报告

    题目地址: https://oj.leetcode.com/problems/interleaving-string/ 题目内容: Given s1, s2, s3, find whether s3  ...

  3. 【CF1132F】Clear the String(动态规划)

    [CF1132F]Clear the String(动态规划) 题面 CF 题解 考虑区间\(dp\). 增量考虑,每次考虑最后一个字符和谁一起删去,然后直接转移就行了. #include<io ...

  4. 【Hihocoder1413】Rikka with String(后缀自动机)

    [Hihocoder1413]Rikka with String(后缀自动机) 题面 Hihocoder 给定一个小写字母串,回答分别把每个位置上的字符替换为'#'后的本质不同的子串数. 题解 首先横 ...

  5. 【CF886D】Restoration of string 乱搞

    [CF886D]Restoration of string 题意:对于给定的一个母串,定义一个字符串是出现频率最多的,当且仅当它在母串中出现的次数最多(可以有多个出现次数最多的,出现的位置可以重叠). ...

  6. 【HDU5421】Victor and String(回文树)

    [HDU5421]Victor and String(回文树) 题面 Vjudge 大意: 你需要支持以下操作: 动态在前端插入一个字符 动态在后端插入一个字符 回答当前本质不同的回文串个数 回答当前 ...

  7. 【CF954I】Yet Another String Matching Problem(FFT)

    [CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...

  8. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  9. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

随机推荐

  1. msgsnd的一个小问题

    今天写了一个System V消息队列的小样例.定义了一个例如以下的结构体: #define MSG_SIZE 8192 struct request { long mtype; int client_ ...

  2. 利用树的先序和后序遍历打印 os 中的目录树

    [0]README 0.1)本代码均为原创,旨在将树的遍历应用一下下以加深印象而已:(回答了学习树的遍历到底有什么用的问题?)你对比下linux 中的文件树 和我的打印结果就明理了: 0.2)我们采用 ...

  3. 10个迷惑新手的Cocoa&Objective-c开发问题

    本文转载至  http://blog.csdn.net/lvxiangan/article/details/27964733     language background run­time thre ...

  4. python 基础 7.6 sys 模块

    一.sys 模块 sys 模块主要功能是获取参数     [root@www pythonscripts]# cat 2.py #!/usr/bin/python #coding=utf-8   im ...

  5. FPGA学习记录 - Quartus II 未使用管脚设置为三态输入

    未使用管脚设置为三态输入 Assignments  -> Device 或双击器件

  6. EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之:内部搜索功能的实现

    上一篇介绍了处理接口获取的数据,本篇将介绍如何在接收到的数据中搜索出自己符合条件的数据: 为了页面的美观,我们往往会以分页的形式来进行数据的展示.但是,当需要展示出来的数据太多的时候,我们很难迅速的找 ...

  7. 关于EF输出sql的执行日志

    sqlserver中可以使用sql profiler:但是mysql当中无法查看:只能借助于组件: ADO.NET Entity Framework CodeFirst 如何输出日志(EF4.3) 用 ...

  8. 【spring配置】——spring整合Quartz定时器

    第一种:为普通java类中的某个方法配置跑批任务 MethodInvokingJobDetailFactoryBean CronTriggerBean SchedulerFactoryBean 1.定 ...

  9. Optimistic concurrency control

    Optimistic concurrency control https://en.wikipedia.org/wiki/Optimistic_concurrency_control Optimist ...

  10. 我的Android进阶之旅------>温习Sqlite3的常用操作

    前言;今天要写一个应用来调节系统的Brightness值,来改变系统的背光亮度.由于刚开始些的时候没有考虑Brightness的最小值,直接托动SeekBar到最小值(为0).瞬间,屏幕变成全黑,失败 ...