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 ...
随机推荐
- BZOJ2649 : riddle
题意同3495,但是内存限制收紧了,不能采用3495的前后缀优化建图的方式. 注意到“每个集合恰好选择一个点”可以放宽成“每个集合最多选择一个点”,对于最后求出的方案里,如果某个集合没选点,任选一个就 ...
- 最简单的原生js和jquery插件封装
最近在开发过程中用别人的插件有问题,所以研究了一下,怎么封装自己的插件. 如果是制作jquery插件的话.就将下面的extend方法换成 $.extend 方法,其他都一样. 总结一下实现原理: 将 ...
- Spring Cloud下微服务权限方案
背景从传统的单体应用转型Spring Cloud的朋友都在问我,Spring Cloud下的微服务权限怎么管?怎么设计比较合理?从大层面讲叫服务权限,往小处拆分,分别为三块:用户认证.用户权限.服务校 ...
- Linux 搭建 Nginx+PHP-FPM环境
安装PHP.Nginx和PHP-FPM sudo apt-get install php sudo apt-get install nginx sudo apt-get install php7-fp ...
- K8s 入门
中文文档:https://www.kubernetes.org.cn/kubernetes%E8%AE%BE%E8%AE%A1%E6%9E%B6%E6%9E%84 小结大白话 Portainer 挺好 ...
- 盒子模型/div标签/益出处理
/* <div></div>没有任何功能,不属于功能标签 可以放文字,图片以及各种元素的块标签 常常用来布局 span标签属于行内标签,无法设置宽高 */ <!docty ...
- unittest批量执行测试用例
现有四个测试用例分别在两个.py文件中,如何执行这些文件? unittest中有这样处理:unittest.TestLoader().discover() 第一个文件test_case1.py fro ...
- Lua学习----零碎知识点
Jit(just in time) 动态即时编译,边运行时边编译---->lua (主要是面向进程) Aot(ahead of time) 静态提前编译,运行前编译---->C#(主要是面 ...
- QEMU KVM Libvirt手册(10): KVM的各种限制
Overcommits KVM allows for both memory and disk space overcommit. However, hard errors resulting fro ...
- SimpleRpc-客户端与服务端工作模型探讨
前言 本篇文章讲述客户端与服务端的具体设计细节.有细心的小伙伴发现,客户端和服务端的工作方式不一样:服务端是多线程计算模型,利用工作线程完成数据的读取,而客户端是单线程(利用Reactor线程完成数据 ...