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.

问题 : 给定三个字符串 s1, s2, s3, 求 s3 是不是由 s1 和 s2 交错组合而成。

感觉是一条比较典型的 DP 题目。

设 i, j, k 分别是 s1, s2, s3 待求解的当前下标。

  • 当 s3[k] != s1[i] 且 s3[k] != s2[j], 则匹配失败
  • 当 s3[k] 只和 s1[i] 相等,则 k++ 和 i++
  • 当 s3[k] 只和 s2[j] 相等,则 k++ 和 j++
  • 当 s3[k] == s1[i] 且 s3[k] == s2[j] ,则分别求 k++ 和 i++ ,以及 k++ 和 j++ ,两者取或运算

由于 s3 恰好有 s1 和 s2 交错行程,所以 s3 长度必然等于 s1 + s2 的长度,知道其中二者就能确定第三个数,这样只需要二维数组保存中间结果即可。

     vector<vector<int>> vv;

     bool interIleave(string s1, string s2, string s3, int p1, int p2, int p3){

     //     cout << s1 << "\n" << s2 << "\n" << s3 << "\n--------\n";

         if (s1.size() == ) {
return (s2 == s3);
} if (s2.size() == ) {
return (s1 == s3);
} if (vv[p1][p3] != -) {
return vv[p1][p3];
} if (s3[] != s1[] && s3[] != s2[]) {
vv[p1][p3] = false;
return false;
} if (s3[] == s1[] && s3[] != s2[]) {
bool tmpb = interIleave(s1.substr(), s2, s3.substr(), p1+, p2, p3+);
vv[p1][p3] = tmpb;
return tmpb;
} if (s3[] != s1[] && s3[] == s2[]) {
bool tmpb = interIleave(s1, s2.substr(), s3.substr(), p1, p2+, p3+);
vv[p1][p3] = tmpb;
return tmpb;
} bool inter1 = interIleave(s1.substr(), s2, s3.substr(), p1+, p2, p3+);
bool inter2 = interIleave(s1, s2.substr(), s3.substr(), p1, p2+, p3+);
bool res = inter1 || inter2; vv[p1][p3] = res; return res;
} bool isInterleave(string s1, string s2, string s3) { vector<vector<int>> tmp(s1.size(), vector<int>(s3.size(), -));
vv = tmp; if (s3.size() != s1.size()+s2.size()) {
return false;
} if (s3.size() == ) {
return true;
} bool res = interIleave(s1, s2, s3, , , ); return res;
}

[LeetCode] Interleaving String 解题思路的更多相关文章

  1. Leetcode:Interleaving String 解题报告

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

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

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

  3. [LeetCode] Word Break 解题思路

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. [LeetCode] Interleaving String [30]

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

  5. [leetcode]Interleaving String @ Python

    原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is ...

  6. [LeetCode] Maximum Gap 解题思路

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

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

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

  8. [Leetcode] Interleaving String

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

  9. [LeetCode] Distinct Subsequences 解题思路

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

随机推荐

  1. 基于HTML5和JSP实现的图片Ajax上传和预览

    本文对如何实现使用Ajax提交"multipart/form"格式的表单数据,已经如何在图片上传之前,在浏览器上进行预览.使用的主要相关技术HTML5的FILE API,XMLHt ...

  2. docker中搭建jenkins环境

    想在docker中搭建一个jenkins环境.开始的时候我想在先pull centos,然后在里面自己搭建环境,搭建后的环境可以运行,但是不知道挂载哪的数据,这也是我不熟悉jenkins的原因. 同事 ...

  3. 【转】Oracle job procedure 存储过程定时任务

    原文:Oracle job procedure 存储过程定时任务 oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. 一.查询系统中的job,可以查询视图 --相 ...

  4. 【转】JSONP简介

     原文链接:说说JSON和JSONP,也许你会豁然开朗,含jQuery用例 先说说JSONP是怎么产生的: 1.一个众所周知的问题,Ajax直接请求普通文件存在跨域无权限访问的问题,甭管你是静态页面. ...

  5. css实现的透明三角形

    css实现下图样式,具体像素值记不住了,很好设置,html code (2014百度秋招面试题): <div id="demo"></div>   分析:这 ...

  6. js在本地预览图片

    移动web <body> <form enctype="multipart/form-data" name="form1"> 上传文件: ...

  7. PHP对URL设置

    一.URL规则  1.默认是区分大小写的  2.如果我们不想区分大小写可以改配置文件   'URL_CASE_INSENSITIVE'=>true,               //url不区分 ...

  8. 解决treeview的同一节点单击多次的执行问题

    在使用treeview的AfterSelect函数的时候,单击同一节点多次无响应. 解决方法: 将首次单击的节点信息保存到selectTreeNode: 然后使用click函数,判断单击的节点是否和s ...

  9. vs2013 IHttpActionResult NotFund Ok (WebAPI)

    vs2013  IHttpActionResult NotFund Ok   使用ASP.NET Web API构造基于restful风格web services,IHttpActionResult是 ...

  10. Js处理json数据

    js中处理由ajax调用返回的json数据问题,可以通过使用JSON.parse方法将json字符串转化成javascript 对象.通过对象访问属性值. JSON.parse 只限于高版本的浏览器. ...