/**
* 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的更多相关文章

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

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

  2. Leetcode:Interleaving String 解题报告

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

  3. [LeetCode] Interleaving String 交织相错的字符串

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

  4. [Leetcode] Interleaving String

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

  5. [LeetCode] Interleaving String 解题思路

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

  6. [LeetCode] Interleaving String [30]

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

  7. [leetcode]Interleaving String @ Python

    原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is ...

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

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

  9. 【leetcode】Interleaving String

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

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

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

随机推荐

  1. CentOS修改yum源

    在安装完CentOS后一般需要修改yum源,才能够在安装更新rpm包时获得比较理想的速度.国内比较快的有163源.sohu源.这里以163源为例子. 1. cd /etc/yum.repos.d 2. ...

  2. NOIP2012提高组day2 T2借教室

    这题骗分可以骗到满分(可能是数据不太强给强行过去了) 这道题如果是按照题意去模拟用循环去修改区间的话只有45分,正解是二分+差分数组,骗分也是差分数组但是没有使用二分,时间复杂度在最坏的情况下是O(n ...

  3. CMD 中常见命令

    引自百度经验:https://jingyan.baidu.com/article/67508eb41d44a09cca1ce4f1.html ipConfig:查询ip ping:查询连接速度: pi ...

  4. vue Error: No PostCSS Config found in

    最近在做一个vue的移动端的项目,遇到一个问题,我本地的项目运行正常,可是上传到github上的一启动就报错,就是标题上的错误,找了很久,刚开始以为是某个css没有配置,就把本地的复制过去还是报错,无 ...

  5. RSA算法加解密

    package org.thcic.ejw.util.encrypt; import java.io.ByteArrayOutputStream; import java.security.Key; ...

  6. Expedition---POJ - 2431

    A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poo ...

  7. XML语言1.简介和语法

    一.什么是XML语言? XML 指可扩展标记语言(Extensible Markup Language) Xml是独立于软件和硬件的信息传输工具. XML 是一种很像HTML的标记语言. 但xml不是 ...

  8. Vue取消eslint语法限制

    话不多说,先上图: 当然,这里的警告我是知道怎么回事,原来eslint是一个语法检查工具,但是限制很严格,在我的vue文件里面很多空格都会导致红线警告(可以屏蔽),虽然可以屏蔽,但是在编译的时候老是会 ...

  9. 微信小程序如何发送短信验证码,无需搭建服务器

    自从微信小程序提供云开发支持,开发者无需搭建后台服务器,使用微信提供的核心API就可以实现应用功能,此时就需要小程序能够自己发送短信,比如短信验证码,榛子云短信(http://smsow.zhenzi ...

  10. for循环语句/命名函数

    for(1.表达式1;2.表达式2;3.表达式3){ 4.循环体语句; } 先执行1,在执行2表达式,如果2的表达式为false的话直接退出循环, 如果2的表达式结果为true,执行4,执行3,执行2 ...