leetcode — interleaving-string
/**
* Source : https://oj.leetcode.com/problems/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.
*
*/
public class InterLeavingString {
/**
* 判断字符串s3是否由s1和s2交织而成
* 直接从s3里面移除s1,然后判断剩下的字符串是否和s2相同,这种做法是行不通的,比如s1="c",s2="ca",s3="cac",
* 如果移除s1,剩下就是ac明显和s2不等,但是s3确实可以由s1和s2交织而成
*
* 这里使用递归的方法解,但是时间复杂度较高
*
* @param s1
* @param s2
* @param s3
* @return
*/
public boolean isInterLeaveByRecursion (String s1, String s2, String s3) {
if (s1.length() + s2.length() != s3.length()) {
return false;
}
int s1Index = 0;
int s2Index = 0;
for (int i = 0; i < s3.length(); i++) {
if ((s1Index < s1.length() && s1.charAt(s1Index) == s3.charAt(i)) && (s2.length() <= s2Index || s2.charAt(s2Index) != s3.charAt(i))) {
s1Index++;
} else if ((s1.length() <= s1Index || s1.charAt(s1Index) != s3.charAt(i)) && (s2.length() > s2Index && s2.charAt(s2Index) == s3.charAt(i))) {
s2Index++;
} else if ((s1.length() > s1Index && s1.charAt(s1Index) == s3.charAt(i)) && (s2.length() > s2Index && s2.charAt(s2Index) == s3.charAt(i))) {
if (!isInterLeaveByRecursion(s1.substring(s1Index + 1), s2.substring(s2Index), s3.substring(i + 1))) {
return isInterLeaveByRecursion(s1.substring(s1Index), s2.substring(s2Index + 1), s3.substring(i + 1));
}
return true;
} else {
return false;
}
}
return s1Index == s1.length() && s2.length() == s2Index;
}
/**
* 使用动态规划解决
*
* 二位数组用来存储s1和s2每一个字符和s3字符的匹配情况,match[s1.length+1][s2.length+1]
* match[i][j]表示s1[0-i],s2[0-j]可以交织成s3[0-(i+j-1)]
* match[i][j] = (match[i-1][j] && s3[i+j-1] == s1[i]) || (match[i][j-1] && s3[i+j-1] == s2[j])
*
* 初始情况:
* i == 0 && j == 0,match[0][0] = true
* i == 0 && j != 0
* s2[j] == s3[j], match[0][j] |= match[0][j-1]
* s2[j] != s3[j], match[0][j] = false
*
* j == 0 && i != 0
* s1[i] == s3[i], match[i][0] |= match[i-1][0]
* s1[i] != s3[i], match[i][0] = false
*
* @param s1
* @param s2
* @param s3
* @return
*/
public boolean isInterleaveByDP (String s1, String s2, String s3) {
if (s1.length() + s2.length() != s3.length()) {
return false;
}
boolean[][] match = new boolean[s1.length()+1][s2.length()+1];
match[0][0] = true;
for (int i = 1; i <= s1.length(); i++) {
if (s1.charAt(i-1) == s3.charAt(i-1)) {
match[i][0] = true;
} else {
break;
}
}
for (int j = 1; j <= s2.length(); j++) {
if (s2.charAt(j-1) == s3.charAt(j-1)) {
match[0][j] = true;
} else {
break;
}
}
for (int i = 1; i <= s1.length(); i++) {
for (int j = 1; j <= s2.length(); j++) {
if (s1.charAt(i-1) == s3.charAt(i+j-1)) {
match[i][j] = match[i-1][j] || match[i][j] ;
}
if (s2.charAt(j-1) == s3.charAt(i+j-1)) {
match[i][j] = match[i][j-1] || match[i][j] ;
}
}
}
return match[s1.length()][s2.length()];
}
public static void main(String[] args) {
InterLeavingString interLeavingString = new InterLeavingString();
System.out.println(interLeavingString.isInterLeaveByRecursion("aabcc", "dbbca", "aadbbcbcac") + "---true");
System.out.println(interLeavingString.isInterLeaveByRecursion("aabcc", "dbbca", "aadbbbaccc") + "---false");
System.out.println(interLeavingString.isInterLeaveByRecursion("c", "ca", "cac") + "---true");
System.out.println(interLeavingString.isInterLeaveByRecursion("", "", "") + "---true");
System.out.println();
System.out.println(interLeavingString.isInterleaveByDP("aabcc", "dbbca", "aadbbcbcac") + "---true");
System.out.println(interLeavingString.isInterleaveByDP("aabcc", "dbbca", "aadbbbaccc") + "---false");
System.out.println(interLeavingString.isInterleaveByDP("c", "ca", "cac") + "---true");
System.out.println(interLeavingString.isInterleaveByDP("", "", "") + "---true");
}
}
leetcode — interleaving-string的更多相关文章
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- [LeetCode] Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- [Leetcode] Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [LeetCode] Interleaving String 解题思路
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [LeetCode] Interleaving String [30]
题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...
- [leetcode]Interleaving String @ Python
原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- LeetCode之“动态规划”:Interleaving String
题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...
随机推荐
- DWM1000 自动应答代码实现与实例
这一节继续继承之前帧过滤部分,首先补充一下关于帧过滤部分,如果将目标地址设置为0xFFFF,则同一个网络(物理频道与PANID 都相同),所有节点都应该收到这条信息,这个信息为广播信息,0xFFFF为 ...
- Hadoop下WordCount程序
一.前言 在之前我们已经在 CenOS6.5 下搭建好了 Hadoop2.x 的开发环境.既然环境已经搭建好了,那么现在我们就应该来干点正事嘛!比如来一个Hadoop世界的HelloWorld,也就是 ...
- pycharm下虚拟环境建立,django项目建立等情况说明
- 超时导致的Galera节点加入集群失败
需求:为galera集群添加新的节点. 初始化新的节点,加入的时候一直报错,加入失败,报错日志如下 WSREP_SST: [ERROR] Removing /var/lib/mysql//.sst/x ...
- IIS 程序池与Site 导出、导入
如何在IIS7或IIS7.5中导入导出站点及应用程序池. 为实现负载平衡,我们可能会使用多个WEB服务器,也就会需要给多个IIS配置同样的站点和应用程序池.那么我们需要一个一个的重新建吗?当然不用,我 ...
- H5测试点总结-UI测试、功能测试、兼容性测试、体验相关(弱网、资源、手机操作等)、安全性测试、性能测试
一.概述 1.1 什么是H5 H5 即 HTML5,是最新的 Web 端开发语言版本,现如今,大多数手机 APP 页面会用 H5 实现,包括 PC Web 站点也会用它开发实现.所以 Web 的通用测 ...
- Tips_钉钉免登前端实现
1.需求:开发钉钉微应用,需要实现钉钉的免登陆功能. #.其实钉钉的文档中心还是很详细的,只是刚开始接触会一头雾水,所以花费了挺多时间....... ?什么是钉钉免登功能. ?企业应用免登开发授权流程 ...
- 巧用PHP中__get()魔术方法
PHP中的魔术方法有很多,这些魔术方法可以让PHP脚本在某些特定的情况下自动调用.比如 __construct() 每次实例化一个类都会先调用该方法进行初始化.这里我们讲一下__get() 魔术方法的 ...
- XSS之偷梁换柱--盲打垃圾短信平台
https://www.t00ls.net/thread-49742-1-1.html
- CentOS7设置SVN自启动,提交报错,无权限.手动kill掉后重启,成功.
参考文档:http://tieba.baidu.com/p/5174054662 最近想尝试在CentOS7上搭建SVN服务.遇到的问题大致如题,我这边再详细描述一下. 虚拟机:VMware® Wor ...