Interleaving String,交叉字符串,动态规划
问题描述:
Given s1, s2, s3, 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,交叉字符串,动态规划的更多相关文章
- lintcode 中等题:interleaving String 交叉字符串
题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...
- 97. Interleaving String(字符串的交替连接 动态规划)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- 097 Interleaving String 交错字符串
给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的.例如,给定:s1 = "aabcc",s2 = "dbbca",当 s ...
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- LeetCode之“动态规划”:Interleaving String
题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...
- 二维动态规划——Interleaving String
97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2 ...
- [LeetCode] Interleaving String [30]
题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...
- 40. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
随机推荐
- Linux chmod命令
chmod用于管理文件或目录的权限,文件或目录权限的控制分别以读取(r).写入(w).执行(x)3种 可读可写可执行,抽象的用二进制来表示 1 代表拥有该权限,0 代表没有该权限,这样我们就可以看到 ...
- mysql应用实例
目录: 表结构 sql练习 1.表结构 SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- T ...
- Jmeter(五)mysql的增删改查
一.导入jdbc的jar包,因为jmeter本身不能直接连接mysql,所以需要导入第三方的jar包,来连接mysql jar包下载地址:https://pan.baidu.com/s/17qQZPF ...
- 在 Sublime Text 2 中编译和运行 Java 程序,以及输出中文出错问题解决办法
Sublime Text 2 是我最喜欢用来编码的文本编辑器,如果你尝试使用后相信你也会喜欢上它的.在这篇文章中我们将讨论如何在 Sublime Text 2 中编译和运行 Java 程序. 第一步: ...
- CCF 201312-4 有趣的数[dp][难]
问题描述 试题编号: 201312-4 试题名称: 有趣的数 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, ...
- Bootstrap 中的插件的学习
一个静态的模态窗口实例,如下面的实例所示: <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 模态 ...
- DevStore分享:月薪3万的程序员都避开了哪些坑
程序员薪水有高有低,有的人一个月可能拿30K.50K,有的人可能只有2K.3K.同样有五年工作经验的程序员,可能一个人每月拿20K,一个拿5K.是什么因素导致了这种差异?我特意总结了容易导致薪水低的九 ...
- iClap分享:如何优雅的在 APP 中实现测试?
开发团队常面临的问题有:内测 APP 时测出一堆 bug 写了很多文档,交到下一个人手中时问题总是不够清晰明了;版本发布公测时只能分发原生版本给团队和用户,无法快速反馈测试和体验结果;使用第三方工具, ...
- Spring-1-I 233 Matrix(HDU 5015)解题报告及测试数据
233 Matrix Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descript ...
- Hive的静态分区和动态分区
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl/p/6831884.html 转载请注明出处 虽然之前已经用过很多次hive的分区表,但是还是找时间快速回顾总结 ...