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.

思路:这一天假设理解思想之后去做感觉不是非常难。可是在想到解法之前还是有一定难度。

本题解法为递归解法,动态规划解法没有掌握。

递归的思路和遍历字符串,切割成两部分,对照两部分能否scramble,只是本题要比較前前和前后两次。

详细代码例如以下:

public class Solution {
public boolean isScramble(String s1, String s2) {
/**
* 思想是递归,将字符串逐个的分为两串
* 然后让两串的前前比較、后后比較,是则返回true
* 不是着前后比較。是则返回true
* 假设还不是。则分割的字符串位置+1
*/ char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray(); //调用函数推断
if(isScr(c1, c2, 0, c1.length, 0, c2.length)){
return true;
} return false;
} boolean isScr(char[] c1,char[] c2,int start1,int end1,int start2, int end2){ //推断字符是否为空
if(end1 - start1 <= 0 && end2 - start2 <= 0){
return true;
}
//假设长度为1。则必须相等
if(end1 - start1 == 1 && end2 - start2 == 1){
return c1[start1] == c2[start2];
}
//长度不等返回false
if( end1 - start1 != end2 - start2){
return false;
} int[] a = new int[128];
//每一个字符串的字符必须个数相等
for(int i = 0; i < end1 - start1; i++){
a[c1[i+start1]]++;
a[c2[i+start2]]--;
} //不相等返回false
for(int i = 0; i < a.length; i++){
if(a[i] != 0){
return false;
}
}
//递归实现
for(int i = 1; i < end1 - start1; i++){
if(isScr(c1, c2, start1, start1+i, start2, start2+i) && isScr(c1, c2, start1+i, end1, start2+i, end2)){
return true;
} if((isScr(c1, c2, start1, start1+i, end2-i, end2) && isScr(c1, c2, start1+i, end1, start2, end2-i))){
return true;
}
}
return false;
}
}

leetCode 87.Scramble String (拼凑字符串) 解题思路和方法的更多相关文章

  1. [LeetCode] 87. Scramble String 搅乱字符串

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

  2. [LeetCode] 87. Scramble String 爬行字符串

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

  3. [leetcode]87. Scramble String字符串树形颠倒匹配

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

  4. [leetcode] 87. Scramble String (Hard)

    题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...

  5. Leetcode#87 Scramble String

    原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...

  6. leetcode@ [87] Scramble String (Dynamic Programming)

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

  7. leetCode 67.Add Binary (二进制加法) 解题思路和方法

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  8. leetCode 86.Partition List(分区链表) 解题思路和方法

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  9. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

随机推荐

  1. wildfly 10的安装部署

    http://www.xue163.com/2203/1/22037981_2.html WildFly 曾用名:JBoss Application Server ,红帽公司宣布 JBoss AS 的 ...

  2. 【基数排序】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) C. Jon Snow and his Favourite Number

    发现值域很小,而且怎么异或都不会超过1023……然后可以使用类似基数排序的思想,每次扫一遍就行了. 复杂度O(k*1024). #include<cstdio> #include<c ...

  3. Java序列化对象为字符串并将字符串反序列化为对象

    对象的序列化与反序列化其实就是将对象的状态保存下来,一般是保存到文件中,但是其实更常用的是将对象序列化为字符串保存到数据库中,然后在需要读取对象的情况下将字符串反序列化为对象.   可以序列化的类必须 ...

  4. MSGPACK序列和还原TFDParams

    MSGPACK序列和还原TFDParams unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, S ...

  5. Vue组件进阶知识总结

    上一篇我们重点介绍了组件的创建.注册和使用,熟练这几个步骤将有助于深入组件的开发.另外,在子组件中定义props,可以让父组件的数据传递下来,这就好比子组件告诉父组件:“嘿,老哥,我开通了一个驿站,你 ...

  6. java中遍历Map几种方法

    java中的map遍历有多种方法,从最早的Iterator,到java5支持的foreach,再到java8 Lambda,让我们一起来看下具体的用法以及各自的优缺点. 先初始化一个map: publ ...

  7. 最简单的PHP socket

    服务端 <?phperror_reporting(E_ALL);set_time_limit(0); $ip = "127.0.0.1";$port = 1935; func ...

  8. Java solr 索引数据增删改查

    具体代码如下: import java.io.IOException; import java.util.*; import org.apache.solr.client.solrj.SolrClie ...

  9. Spark集群数据处理速度慢(数据本地化问题)

    SparkStreaming拉取Kafka中数据,处理后入库.整个流程速度很慢,除去代码中可优化的部分,也在spark集群中找原因. 发现: 集群在处理数据时存在移动数据与移动计算的区别,也有些其他叫 ...

  10. iovec结构体定义及使用 (转)

    I/O向量(struct iovec) readv(2)与writev(2)函数都使用一个I/O向量的概念.这是由所包含的文件定义的: #include <sys/uio.h> 头文件定义 ...