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 ...
随机推荐
- js合并table指定列
function MergeTableCell(tableId, startRow, endRow, col) { var tb = document.getElementById(tableId); ...
- python爬虫实践(二)——爬取张艺谋导演的电影《影》的豆瓣影评并进行简单分析
学了爬虫之后,都只是爬取一些简单的小页面,觉得没意思,所以我现在准备爬取一下豆瓣上张艺谋导演的“影”的短评,存入数据库,并进行简单的分析和数据可视化,因为用到的只是比较多,所以写一篇博客当做笔记. 第 ...
- PeopleSoft 后台更新密码
一.SQL脚本 where t.oprid='&opridName' ; 二.Data Mover 1.指定用户加密ENCRYPT_PASSWORD 用户名;2.所有用户ENCRYPT_PAS ...
- 学习如何设置ssh安全只允许用户从指定的IP登陆
参考原文链接:https://www.cnblogs.com/wuling129/p/5076081.html 因为在公司做项目时,员工的登录可能受到外界人的窃取,为了防止被攻击,设置系统只能允许用户 ...
- 一文教你看懂大数据的技术生态圈:Hadoop,hive,spark
转自:https://www.cnblogs.com/reed/p/7730360.html 大数据本身是个很宽泛的概念,Hadoop生态圈(或者泛生态圈)基本上都是为了处理超过单机尺度的数据处理而诞 ...
- Java工作原理:JVM,内存回收及其他
JAVA虚拟机系列文章 http://developer.51cto.com/art/201001/176550.htm Java语言引入了Java虚拟机,具有跨平台运行的功能,能够很好地适应各种We ...
- vim 常用指令
其他命令 <c-L> 重绘屏幕 <c-z> 挂起vim回到shell,想继续vim只需要输入 fg <c-x-f> 文件路径提示 <c-N> 当前文件中 ...
- Java script 逻辑运算符
a && b : 将a, b转换为Boolean类型, 再执行逻辑与, true返回b, false返回a 1.只要“&&”前面是false,无论“&& ...
- python语法_列表生成器_生成器_迭代器_异常捕获
列表生成式 a = [x for x in range(10)] print(a) x 可进行操作 a = [x*2 for x in range(10)] print(a) x甚至可以为函数, de ...
- [译文]Domain Driven Design Reference(七)—— 大型战略设计结构
本书是Eric Evans对他自己写的<领域驱动设计-软件核心复杂性应对之道>的一本字典式的参考书,可用于快速查找<领域驱动设计>中的诸多概念及其简明解释. 上周末电脑硬盘文件 ...