import java.util.Arrays;

/**
* Source : https://oj.leetcode.com/problems/scramble-string/
*
* Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.
*
* Below is one possible representation of s1 = "great":
*
* great
* / \
* gr eat
* / \ / \
* g r e at
* / \
* a t
*
* To scramble the string, we may choose any non-leaf node and swap its two children.
*
* For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".
*
* rgeat
* / \
* rg eat
* / \ / \
* r g e at
* / \
* a t
*
* We say that "rgeat" is a scrambled string of "great".
*
* Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".
*
* rgtae
* / \
* rg tae
* / \ / \
* r g ta e
* / \
* t a
*
* We say that "rgtae" is a scrambled string of "great".
*
* Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.
*
*/
public class ScrambleString { /**
* s1是不是s2的一个scramblestring
* s1按照任意位置进行二分划分,一直递归下去,期间,可以交换非叶子节点的两个子节点左右顺序,一直到叶子节点
*
* 一开始想着是找到s1的所有scramblestring,然后判断s2是否在里面
* 但是其实在寻找s2的scramblestring的时候就可以和s2进行对比判断,而不需要存储所有的scramblestring
*
* 选择
* s1分割的位置,递归的进行如下判断
* s1在i左边的子串和s2在i左边的子串是scramble的,s1在i的右边的子串和s2在i右边的子串是scramble的,或者
* s1在i左边的子串和s2在i右边的子串是scramble的,s1在i的右边的子串和s2在i左边的子串是scramble的
*
* @param s1
* @param s2
*/
public boolean scramble (String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
if (s1.length() <= 1) {
return s1.equals(s2);
}
// return recursion(s1, s2);
return recursion1(s1, s2);
} public boolean recursion (String s1, String s2) {
int len = s1.length();
if (len == 1) {
return s1.equals(s2);
}
for (int i = 1; i < len; i++) {
if ((recursion(s1.substring(0, i), s2.substring(0, i)) && recursion(s1.substring(i), s2.substring(i)))
|| (recursion(s1.substring(0,i), s2.substring(len-i)) && recursion(s1.substring(i), s2.substring(0,len-i)))) {
return true;
}
}
return false;
} /**
* 递归的时候有些分支是不必要的,可以剪裁分支
* 在递归的时候,对s1和s2进行排序,如果排序之后两个字符串不相等则不必要继续递归
*
* @param s1
* @param s2
* @return
*/
public boolean recursion1 (String s1, String s2) {
int len = s1.length();
if (len == 1) {
return s1.equals(s2);
}
char[] s1CharArr = s1.toCharArray();
Arrays.sort(s1CharArr);
String sortedS1 = new String(s1CharArr);
char[] s2CharArr = s2.toCharArray();
Arrays.sort(s2CharArr);
String sortedS2 = new String(s1CharArr);
if (!sortedS1.equals(sortedS2)) {
return false;
} for (int i = 1; i < len; i++) {
if ((recursion(s1.substring(0, i), s2.substring(0, i)) && recursion(s1.substring(i), s2.substring(i)))
|| (recursion(s1.substring(0,i), s2.substring(len-i)) && recursion(s1.substring(i), s2.substring(0,len-i)))) {
return true;
}
}
return false;
} /**
* 递归的时候会有一些重复计算,使用数组记录计算过的结果,每次递归的时候判断,如果已经计算过则直接使用计算过的结果
* 中间结果需要一个三维的boolean数组,因为,每次计算结果的变量是s1.index1,s2.index2,还有当前字符串的长度len
*
* @param s1
* @param s2
* @return
*/
public boolean scramble2 (String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
if (s1.length() <= 1) {
return s1.equals(s2);
}
int[][][] calculated = new int[s1.length()][s2.length()][s1.length()];
for (int i = 0; i < s1.length(); i++) {
for (int j = 0; j < s2.length(); j++) {
Arrays.fill(calculated[i][j], -1);
}
}
return recursion(s1, s2);
} public boolean recursion2 (String s1, int index1, String s2, int index2, int len, int[][][] calculated) {
if (len == 1) {
return s1.charAt(index1) == s2.charAt(index2);
}
int preresult = calculated[index1][index1][len-1];
if (preresult != -1) {
return preresult == 1;
}
preresult = 0;
for (int i = 1; i < len; i++) {
if (recursion2(s1, index1, s2, index2, i, calculated)
&& recursion2(s1, index1 + 1, s2, index2 + 1, len - i, calculated)) {
preresult = 1;
break;
}
if (recursion2(s1, index1, s2, index2 + len - i, i, calculated)
&& recursion2(s1, index1 + 1, s2, index2, len - i, calculated)) {
preresult = 1;
break;
}
}
calculated[index1][index2][len-1] = preresult;
return preresult == 1;
} public static void main(String[] args) {
ScrambleString scrambleString = new ScrambleString();
System.out.println(scrambleString.scramble("great", "rgtae"));
System.out.println(scrambleString.scramble2("great", "rgtae"));
}
}

leetcode — scramble-string的更多相关文章

  1. Leetcode:Scramble String 解题报告

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  2. [LeetCode] Scramble String -- 三维动态规划的范例

    (Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...

  3. [LeetCode] Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  4. [leetcode]Scramble String @ Python

    原题地址:https://oj.leetcode.com/problems/scramble-string/ 题意: Given a string s1, we may represent it as ...

  5. [Leetcode] Scramble String

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  6. [LeetCode] Scramble String(树的问题最易用递归)

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  7. [Leetcode] scramble string 乱串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  8. [LeetCode] Scramble String 字符串 dp

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  9. 【一天一道LeetCode】#87. Scramble String

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

  10. 【leetcode】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

随机推荐

  1. hadoop ha zkfc 异常自动切换机制和hdfs 没有空间问题解决

    在我搭建hadoop ha 后,我启动了各个功能,但是发现hadoop hdfs 没法使用,在web 页面也显示hdfs 可用空间为零,并且自动备份机制无法使用,本人也不理解,然后就是指定hdfs t ...

  2. [开源] C# 封装 银海医保的接口

    Github 地址: https://github.com/zifeiniu/YinHaiYiBaoCSharpAPI C#Model封装 银海医保的接口 介绍 银海医保的接口我就不说了,很多家医院在 ...

  3. Java String类的intern()方法

    该方法的作用是把字符串加载到常量池中(jdk1.6常量池位于方法区,jdk1.7以后常量池位于堆) 在jdk1.6中,该方法把字符串的值复制到常量区,然后返回常量区里这个字符串的值: 在jdk1.7里 ...

  4. 实现logstash6.4.3 同步mysql数据到Elasticsearch6.4.3

    本文旨在实践把mysql已有的数据同步到elasticsearch中,使用的版本是6.4.3,对于其它6.x版本理应是一样的处理方式. 本文目录: 1.初始化Elasticsearch 6.4.3 1 ...

  5. MyBatis(九) 使用association定义单个对象的封装规则

    (1)接口中编写方法 public Emp getEmpandDept(); (2)编写Mapper文件 <resultMap type="com.eu.bean.Emp" ...

  6. python学习笔记(3)

    .................................................................................................... ...

  7. vue路由传参的三种方式区别(params,query)

    最近在做一个项目涉及到列表到详情页的参数的传递,网上搜索一下路由传参,结合自己的写法找到一种适合自己的,不过也对三种写法都有了了解,在此记录一下 <ul class="table_in ...

  8. XGBoost原理和公式推导

     本篇文章主要介绍下Xgboost算法的原理和公式推导.关于XGB的一些应用场景在此就不赘述了,感兴趣的同学可以自行google.下面开始: 1.模型构建 构建最优模型的方法一般是最小化训练数据的损失 ...

  9. golang ntp协议客户端

    NTP(Network Time Protocol,网络时间协议)是由RFC 1305定义的时间同步协议,用来在分布式时间服务器和客户端之间进行时间同步.NTP基于UDP报文进行传输,使用的UDP端口 ...

  10. mysql数据库内容相关操作

    第一:介绍 mysql数据内容的操作主要是: INSERT实现数据的插入 UPDATE实现数据的更新 DLETE实现数据的删除 SELECT实现数据的查询. 第二:增(insert) 1.插入完整的数 ...