/**
* 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. MyBatis3系列__05查询补充&resultMap与resultType区别

    1.查询补充 当你查询一条记录并且是简单查询时,情况相对简单,可以参考以下的例子: public Employee getEmpById(Integer id); 对应的xml文件中: <sel ...

  2. PS2键盘扫描码:通码与断码

    键盘扫描码(实用于标准PC的101.102和104 键的键盘),按下发送通码,弹起发送断码. 说明: 第一类按键,通码为1字节,断码为 0xF0+通码 形式.如A键,其通码为 0x1C,断码为 0xF ...

  3. VS之设置文件编码格式

    VS2012默认格式为 "GB2312-80",很多时候可能出现乱码情况,就是编码问题,如何在VS里修改呢? 文件->“高级保存选项 ”  选择gb2312

  4. 配置Https 和 HSTS

    1. 视频 https://www.bilibili.com/video/av33344382/?p=2 using System; using Microsoft.AspNetCore.Builde ...

  5. [LeetCode] Leaf-Similar Trees 叶结点相似的树

    Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form ...

  6. python+SQLAlchemy+爬虫

    python+SQLAlchemy+爬虫 前面分享了SQLAlchemy的知识,这次我共享一下学习用python开发爬虫再把爬出来的数据放到用SQLAlchemy的数据库上面的知识,当然我这个是带测试 ...

  7. gdbserver移植到DM368板子上的过程 以及segment fault problem

    问题描述 我在PC机上安装了gdbserver,但是移植到板子上后却出现了问题.运行不了,显示错误:"segment fault". 决定重新在另一台虚拟机上gdbserver. ...

  8. 业务配置开发平台qMISPlat 2.0 产品介绍

    qMISPlat是什么 qMISPlat(业务配置开发平台)是一套基于.net core 2.0.跨平台的,面向开发人员和具有一定技术水平的业务人员使用的业务配置开发平台.基于此平台您只需通过配置和少 ...

  9. 合适么?现在学ASP.NET Core入门编程……

    现在都快找不到ASP.NET的培训课程了. 知道我要开课做培训,有同学劝我:“憋讲那什么.NET,讲Java,现在这个火!”我说我Java不熟,“唉呀!C#转Java,分分钟的事!以飞哥你的经验,…… ...

  10. vue-router路径计算问题

    简书原文 昨天刚刚发表了一个前端跨域新方案尝试,今天在开发中就遇到的了问题. 起因 前端使用的是vue-router组件的history模式,但是由于我们的整个页面都是从static(静态资源站)lo ...