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.

算法:使用动态规划,f[i][j]表示s1的前i个字符和s2的前j个字符是否能组成s3的前i+j个字符,f[i][i]=true,条件:f[i-1][j]=true&&s1[i-1]==s3[i+j-1]或者f[i][j-1]=true&&s2[j-1]=s3[i+j-1]  不满足条件则:f[i][j]=false;最后f[s1.size()][s2.size()]即为所求,代码如下:(一开始被各种下标绕晕了,想了半天终于想明白了)

 1 class Solution {
 2 public:
 3     bool isInterleave(string s1, string s2, string s3) {
 4       int len1=s1.size();
 5       int len2=s2.size();
 6       int len3=s3.size();
 7       if(len1+len2!=len3)  return false;
 8       bool f[len1+1][len2+1];
 9       f[0][0]=true;
10       for(int i=1;i<=len1;i++)
11       {
12           if(f[i-1][0]&&s1[i-1]==s3[i-1])  f[i][0]=true;
13           else f[i][0]=false;
14       }
15       for(int i=1;i<len2+1;i++)
16       {
17           if(f[0][i-1]&&s2[i-1]==s3[i-1])  f[0][i]=true;
18           else f[0][i]=false;
19       }
20
21       for(int i=1;i<=len1;i++)
22       {
23           for(int j=1;j<=len2;j++)
24           {
25               if(s1[i-1]==s3[i+j-1]&&f[i-1][j]||s2[j-1]==s3[i+j-1]&&f[i][j-1])  f[i][j]=true;
26               else f[i][j]=false;
27           }
28       }
29       return f[len1][len2];
30     }
31 };

转载于:https://www.cnblogs.com/sqxw/p/4007977.html

Interleaving String相关推荐

  1. 【重点!DP】LeetCode 97. Interleaving String

    LeetCode 97. Interleaving String 参考网址:[1]https://www.youtube.com/watch?v=HmAF9xeS_2I [2]http://www.c ...

  2. LeetCode Interleaving String(动态规划)

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

  3. 【Lintcode】029.Interleaving String

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

  4. leetcode - Interleaving String

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

  5. [LeetCode] Interleaving String

    1. 是一个很明显的动态规划题. 2. s3中的每个字符不是s1中的就是s2中的,只要根据它之前的状态做转移就可以. 1 class Solution { 2 public: 3 bool isInt ...

  6. LeetCode Interleaving String

    原题链接在这里:https://leetcode.com/problems/interleaving-string/ 这是一道DP题,用boolean数组dp维护历史数据,dp[i][j]表示长度为i ...

  7. 【leetcode】97. Interleaving String

    题目如下: 解题思路:本题可以采用动态规划的方法.记dp[i][j]表示用s1的前i个字符和s2的前j个字符能否组成s3的前(i+j)个字符,如果dp[i-1][j]是True的话,只要s1[i] = ...

  8. 97. Interleaving String 交错字符串

    Title 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = "dbb ...

  9. leetcode解题报告:Interleaving String

    问题描述: 给定两个字符串s1, s2,判断字符串s3是否是由s1.s2交错构成. 例子: s1 = "aabcc", s2 = "dbbca", When s ...

最新文章

  1. 海思3536:PC客户端编译过程报错及解决方法
  2. 解读Go语言的2018:怎么就在中国火成这样了?
  3. JavaScript常用DOM集合
  4. exportfs命令、NFS客户端问题、FTP介绍、使用vsftpd搭建ftp
  5. 数据中心液体冷却方案正在兴起的五大原因
  6. Serverless在编程教育中的实践
  7. java编程基础素数实验报告,JAVA 基础编程练习题1 (输出素数)
  8. 数据挖掘实战项目-客户流失及客户行为偏好分析
  9. wget在linux中安装出现错误解决办法
  10. 手写bind_一次搞定前端“四大手写”
  11. 拓端tecdat|R语言广义相加(加性)模型(GAMs)与光滑函数可视化
  12. valgrind 内存泄露检测
  13. BOS 系统 定区关联客户功能实现
  14. 关于我的一些学习感悟
  15. nacos cloud 配置中心中 修改properties格式内容并发布然后在浏览器页面刷新无效果
  16. python3.6 exe_详解Python3.6的py文件打包生成exe
  17. Automatic Metallic Surface Defect Detection and Recognition with Convolutional NeuralNetworks-论文阅读笔记
  18. js实现oss批量下载文件_jquery批量下载文件
  19. Linux权限drwxrwxrwx是什么意思?
  20. 1425 加工生产调度

热门文章

  1. 队列与环形队列使用数组模拟
  2. [RN] React Native 调试技巧
  3. SpringBoot专栏(一) -- SpringBoot简介
  4. 塔式Server 服务器ESXI6.5安装
  5. History(历史)命令用法 15 例
  6. jQuery动态改变图片显示大小(修改)
  7. Java零基础如何去学习Java编程
  8. 自学Java和Java培训班的学习效果
  9. webpack与vue环境搭建(转载)
  10. iOS中Block的基础用法