问题描述:

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.

算法分析:

“When you see string problem that is about subsequence or matching, dynamic programming method should come to your mind naturally. ”

此题可以用递归,也可以用动态规划。字符串的题一般用动态规划。

dp[i][j]表示s1取前i位,s2取前j位,是否能组成s3的前i+j位

举个列子,注意左上角那一对箭头指向的格子dp[1][1], 表示s1取第1位a, s2取第1位d,是否能组成s3的前两位aa

从dp[0][1] 往下的箭头表示,s1目前取了0位,s2目前取了1位,我们添加s1的第1位,看看它是不是等于s3的第1位,( i + j 位)

从dp[1][0] 往右的箭头表示,s1目前取了1位,s2目前取了0位,我们添加s2的第1位,看看它是不是等于s3的第1位,( i + j 位)

首先第一个条件,新添加的字符,要等于s3里面对应的位( i + j 位),第二个条件,之前那个格子也要等于True

举个简单的例子s1 = ab, s2 = c, s3 = bbc ,假设s1已经取了2位,c还没取,此时是False(ab!=bb),我们取s2的新的一位c,即便和s3中的c相等,但是之前是False,所以这一位也是False

同理,如果s1 = ab, s2 = c, s3=abc ,同样的假设,s1取了2位,c还没取,此时是True(ab==ab),我们取s2的新的一位c,和s3中的c相等,且之前这一位就是True,此时我们可以放心置True (abc==abc)

public class InterleavingString
{
//递归
public boolean isInterleave(String s1, String s2, String s3)
{
if(s1.length() == 0)
{
return s2.equals(s3);//不能用==判断,否则出错
}
if(s2.length() == 0)
{
return s1.equals(s3);
}
if(s3.length() == 0)
{
return s1.length() + s2.length() == 0;
} if(s1.charAt(0) == s3.charAt(0) && s2.charAt(0) != s3.charAt(0))
{
return isInterleave(s1.substring(1), s2, s3.substring(1));
}
else if(s2.charAt(0) == s3.charAt(0) && s1.charAt(0) != s3.charAt(0))
{
return isInterleave(s1, s2.substring(1), s3.substring(1));
}
else if(s1.charAt(0) == s3.charAt(0) && s2.charAt(0) == s3.charAt(0))
{
return isInterleave(s1.substring(1), s2, s3.substring(1)) || isInterleave(s1, s2.substring(1), s3.substring(1));
}
else
{
return false;
}
} //动态规划
public boolean isInterleave2(String s1, String s2, String s3)
{
if(s1 == null || s2 == null || s3 == null) return false;
if(s1.length() + s2.length() != s3.length()) return false;
boolean[][] dp = new boolean[s1.length()+1][s2.length()+1];
dp[0][0] = true;
for(int i = 1; i < s1.length() + 1; i ++)
{
if(s1.charAt(i-1) == s3.charAt(i-1) && dp[i-1][0])
{
dp[i][0] = true;
}
}
for(int j = 1; j < s2.length() + 1; j ++)
{
if(s2.charAt(j-1) == s3.charAt(j-1) && dp[0][j-1])
{
dp[0][j] = true;
}
} for(int i = 1; i < s1.length() + 1; i ++)
{
for(int j = 1; j < s2.length() + 1; j ++)
{
if(s2.charAt(j-1) == s3.charAt(i+j-1) && dp[i][j-1])
{
dp[i][j] = true;
}
if(s1.charAt(i-1) == s3.charAt(i+j-1) && dp[i-1][j])
{
dp[i][j] = true;
}
}
}
return dp[s1.length()][s2.length()];
} public static void main(String[] args)
{
String s1 = "aabcc";
String s2 = "dbbca";
String s3 = "aadbbcbcac";
String s4 = "aadbbbaccc";
InterleavingString lv = new InterleavingString();
System.out.println(lv.isInterleave2(s1, s2, s3));
System.out.println(lv.isInterleave2(s1, s2, s4));
}
}

Interleaving String,交叉字符串,动态规划的更多相关文章

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

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

  2. 97. Interleaving String(字符串的交替连接 动态规划)

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

  3. 097 Interleaving String 交错字符串

    给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的.例如,给定:s1 = "aabcc",s2 = "dbbca",当 s ...

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

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

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

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

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

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

  7. [LeetCode] Interleaving String [30]

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

  8. 40. Interleaving String

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

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

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

  10. Leetcode:Interleaving String 解题报告

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

随机推荐

  1. windows 下 方便工作的bat文件批处理命令

    1.删除目录下 不包含某串字符的文件: @echo offfor /f "delims=" %%a in ('dir /s /a-d/b *.mp3') do ( echo &qu ...

  2. Python中的高级数据结构(转)

    add by zhj: Python中的高级数据结构 数据结构 数据结构的概念很好理解,就是用来将数据组织在一起的结构.换句话说,数据结构是用来存储一系列关联数据的东西.在Python中有四种内建的数 ...

  3. json中load和loads区别

    相同点 dump 和 dumps 都实现了序列化 load 和 loads 都实现反序列化 变量从内存中变成可存储或传输的过程称之为序列化序列化是将对象状态转化为可保存或可传输格式的过程. 变量内容从 ...

  4. 图解HTTP之HTTPS详解

    背景:随着越来越多的主流网站已经使用了HTTPS,作为服务器端开发者,就必须了解HTTPS的优势与劣势. 在HTTP协议中有可能存在信息窃听或身份伪装等问题,而使用HTTPS通信机制可以有效地防止这些 ...

  5. HTML5开源RPG游戏引擎lufylegendRPG 1.0.0发布

    经历了几个月的改进,终于发布1.0.0版了.虽然引擎依然存在漏洞,但是比起上次更新还是要好多了.在这里不得不感谢各位网友的大力支持. 首先为引擎做一个开场白吧,也好让大家了解一下它: lufylege ...

  6. 脱离JVM? Hadoop生态圈的挣扎与演化

    本文由知乎<大数据应用与实践>专栏 李呈祥授权发布,版权所有归作者,转载请联系作者! 新世纪以来,互联网及个人终端的普及,传统行业的信息化及物联网的发展等产业变化产生了大量的数据,远远超出 ...

  7. hashmap,ConcurrentHashMap与hashtable的区别

    1.hashmap与hashtable的区别 1.我们从他们的定义就可以看出他们的不同,HashTable基于Dictionary类,而HashMap是基于AbstractMap.Dictionary ...

  8. python爬虫中文乱码解决方法

    python爬虫中文乱码 前几天用python来爬取全国行政区划编码的时候,遇到了中文乱码的问题,折腾了一会儿,才解决.现特记录一下,方便以后查看. 我是用python的requests和bs4库来实 ...

  9. 推荐系统第5周--- 基于内容的推荐,隐语义模型LFM

    基于内容的推荐

  10. ios元素定位

    原文地址http://www.cnblogs.com/meitian/p/7373460.html 第一种:通过Appium1.6的Inspector来查看 具体安装方式前面的随笔已经介绍了:http ...