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. CodeChef - UASEQ Chef and sequence

    Read problems statements in Mandarin Chinese and Russian. You are given an array that consists of n ...

  2. AOJ 2230 How to Create a Good Game(费用流)

    [题目链接] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2230 [题目大意] 给出一张图,从1到n的最长路不变的情况下, 还能 ...

  3. 【动态规划】【spfa】【最短路】bzoj1003 [ZJOI2006]物流运输trans

    预处理cost[a][b] 表示第a天到第b天用同一条线路的成本. 具体转移看代码. #include<cstdio> #include<algorithm> #include ...

  4. 【二维偏序】【树状数组】【权值分块】【分块】poj2352 Stars

    经典问题:二维偏序.给定平面中的n个点,求每个点左下方的点的个数. 因为 所有点已经以y为第一关键字,x为第二关键字排好序,所以我们按读入顺序处理,仅仅需要计算x坐标小于<=某个点的点有多少个就 ...

  5. [TC-HouseProtection]House Protection

    题目大意: 一个平面直角坐标系中有给定的$n(n\le50)$个红点和$m(m\le50)$个蓝点,每个点可以选择画一个半径为$r$(所有的$r$相同)的圆或不画.圆的半径上限为$R(R\le1000 ...

  6. java 面向接口编程的理解

    初学者可能在学习中会有很多疑惑,为什么要这样,明明可以那样实现,这样做的好处又是什么? 可能会的人觉得很简单很容易理解,甚至可能觉得问的问题很智障,但对于小白来说可能是苦思冥想都不得其解的. 自己身为 ...

  7. Spring学习——DI(依赖注入)

    IOC容器,处理对象依赖关系 IOC与DI: IOC :是一个容器,创建对象的容器 DI :在容器创建对象后,处理对象的依赖关系,也叫依赖注入! 方式1:通过set方法注入值 可以给普通属性.集合属性 ...

  8. python 文件操作与集合

    对文件的操作 1.打开文件,获取句柄 2.根据句柄操作文件 3.关闭文件 现有文档 poem.txt 一天很短, 短得来不及拥抱清晨, 就已经手握黄昏. 一年很短, 短得来不及细品初春殷红窦绿, 就要 ...

  9. Shell实现多级菜单系统安装维护脚本实例分享

    Shell实现多级菜单系统安装维护脚本实例分享 这篇文章主要介绍了Shell实现多级菜单系统安装维护脚本实例分享,本文脚本用多级菜单实现管理WEB服务器.Mysql服务器.Nginx服器等,需要的朋友 ...

  10. Ueditor .net版安装配置打开项目的源码傻瓜版教程 亲測~

    环境要求: 没有 .NET Framework 4.0的要先安装 安装完 .NET Framework 4.0 后.还须要向 IIS 注冊应用程序池,注冊的方法是,使用管理员权限打开命令提示符(CMD ...