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. MAC 设置环境变量path的几种方法

mac 一般使用bash作为默认shell Mac系统的环境变量,加载顺序为:/etc/profile /etc/paths ~/.bash_profile ~/.bash_login ~/.prof ...

2. V-rep学习笔记：机器人逆运动学数值解法（The Pseudo Inverse Method）

There are two ways of using the Jacobian matrix to solve kinematics. One is to use the transpose of ...

3. （笔记）angular选中变色

4. 软件工程实践小队--团队项目NABC

团队项目的NABC 1) N (Need 需求) 作为一个网上教学问答系统,用户的基本需求很明确,即为:提问.搜索.浏览.回答.编辑.评论.附加需求还有: 获取金币.提升等级. 提问:关于一门学科,用 ...

5. 【BZOJ】【4004】【JLOI2015】装备购买

拟阵/贪心 题解戳这里:http://blog.csdn.net/popoqqq/article/details/45148309 思路: 裸拟阵…… 维护线性基,将武器按价格排序,从小到大塞进去,如 ...

6. HTML标签大全

HTML标签解释大全 一.HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(DTD). 标签:a 说明:标明超链接的起始或目的位置. 标签:acronym 说明:标 ...

7. Java编程的逻辑 (87) - 类加载机制

本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

8. Spring启动异常: cvc-elt.1: Cannot find the declaration of element &#39;beans&#39;(转)

Spring启动异常: cvc-elt.1: Cannot find the declaration of element 'beans' 2008-09-07 22:41 今天把在线聊天室代码改了下 ...

9. 协程（Coroutine）并不是真正的多线程

自:http://www.zhihu.com/question/23895384 说到Coroutine,我们必须提到两个更远的东西.在操作系统(os)级别,有进程(process)和线程(threa ...

10. python .dcm文件读取，并转化为.jpg格式

.dcm文件是DICOM(Digital Imaging and Communications in Medicine)即医学数字成像和通信中记录医学图像和相关信息的文件,在用于医学图像处理的时候我们 ...