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```

s2 分成 s2Left 和 s2Right 两部分

``` class Solution {
public boolean isScramble(String s1, String s2) {
// corner
if(s1.length() != s2.length() || s1 == null || s2 == null) return false;
// recursion 出口
if(s1.equals(s2)) return true; // 一定要加，否则recursion 没有出口
// valid anagram or not
int[]map = new int[256];
for(int i = 0; i < s1.length(); i++){
map[s1.charAt(i)] ++;
map[s2.charAt(i)] --;
}
for(int i : map){
if( i != 0){
return false;
}
}
// recursion
for(int i = 1; i < s1.length(); i++){
if(isScramble(s1.substring(0,i), s2.substring(0,i)) && isScramble(s1.substring(i), s2.substring(i))) return true;
if(isScramble(s1.substring(0,i), s2.substring(s2.length()-i)) && isScramble(s1.substring(i), s2.substring(0, s2.length()-i))) return true;

}
return false;
}

}```

## [leetcode]87. Scramble String字符串树形颠倒匹配的更多相关文章

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

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

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 （拼凑字符串） 解题思路和方法

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

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】#87. Scramble String

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

8. 【leetcode】Scramble String

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

9. 【LeetCode】87. Scramble String

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

## 随机推荐

1. iOS截屏

- (UIImage *)captureImageFromView:(UIView *)view{ UIGraphicsBeginImageContext(view.bounds.size); CGC ...

2. 初探Team Foundation Server （TFS） 2015 REST API

REST是一种简洁方便的Web服务,通过基于http协议的远程通信,可以为多种客户端程序提供远程服务,大幅提高了服务器系统的可扩展性. 微软宣布从Team Foundation Server 从201 ...

3. Windows下LDAP服务器配置

LDAP即轻量级目录访问协议(Lightweight Directory Access Protocol),基础知识不再赘述,本文主要记录我的配置与安装过程. LDAP for windows下载 o ...

4. locale------- linux字符集

查看当前系统字符集 [root@server1 ~]# locale LANG=zh_CN.UTF-8LC_CTYPE="zh_CN.UTF-8"LC_NUMERIC=" ...

5. Non-unique Elements

Non-unique Elements You are given a non-empty list of integers (X). For this task, you should return ...

6. KVC与KVO理解

转载:https://magicalboy.com/kvc_and_kvo/ KVC 与 KVO 理解 KVC 与 KVO 是 Objective C 的关键概念,个人认为必须理解的东西,下面是实例讲 ...

7. 通过服务修改widgetUI

public static void updateAppWidget(Context context, String displayMsg) { AppWidgetManager appWidgetM ...

8. js用replaceAll全部替换的方法

1 前言 js中字符串整体替换,只有自带的replace,并没有replaceAll,如果我们需要把字符串中的字符统一替换,可以用正则表达式,由于经常使用就在String直接加个原生方法,方便调用. ...

9. PHP 编程小点

1.全局变量,\$global 2.引用.函数返回引用.写时拷贝.对象复制.clone.unset.\$varname=null 3.函数作用域,但是没有块级作用域 4.php7 新特性 5.autolo ...

10. vue实践中的狗血事件之：mock数据引发的血坑

在项目实践中,遇到了这么一档子事 开发环境下,很快乐,什么事儿都没有,于是想打包一下测一下自动登录的效果 好家伙,一开始登录没有效,改来改去,最后连路由都切换不了, 明明开发环境下好好的,为毛打包后就 ...