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.

class Solution {
public:
bool isScramble(string s1, string s2) {
if(s1.size()== || s2.size()==)
return false;
if(s1 == s2)
return true;
string a1 = s1,a2 = s2;
sort(a1.begin(),a1.end());
sort(a2.begin(),a2.end());
if(a1!= a2)
return false;
int len = s1.size();
for(int n = ;n < len;n++){
if(isScramble(s1.substr(,n),s2.substr(,n)) && isScramble(s1.substr(n,len-n),s2.substr(n,len-n)))
return true;
if(isScramble(s1.substr(,n),s2.substr(len-n,n)) && isScramble(s1.substr(n,len-n),s2.substr(,len-n)))
return true; }//end for
return false;
}//end func
};

[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 字符串 dp

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

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

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

  9. 【leetcode】Scramble String

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

随机推荐

  1. BZOJ1175 : [Balkan2007]The stairways of Saharna

    杨氏图表,维护若干个单调不下降队列. 每次新加入一个数时,先考虑第一个队列: 如果可以放在最后,则放在最后. 否则找到最小的可以替换的替换掉,再将替换的数放入第二个队列,以此类推. 最后$ans_i= ...

  2. BZOJ2725 : [Violet 6]故乡的梦

    如果S==T,那么答案为0. 如果S与T不连通,那么答案为inf. 否则,S到T的最短路径上至少有一条边. 求出以S为源点的最短路图,是个DAG,随便抓一条S到T的最短路,记为P. 设dpS[x]表示 ...

  3. javascript为元素绑定事件响应函数

    javascript中为元素设置响应时间有两种方法. (1):object.onclick=functionName; 这种方法不可以传递参数. (2):object.onclick=function ...

  4. 更新Android SDK之后Eclipse提示ADT版本过低的一个简易解决办法

    首先说明一下发表这一篇博文的“历史原因”吧,因为在更新SDK之后,进入Eclipse设置Android SDK目录的时候,会突然说我的版本低什么的,尝试自己解决但失败之后,我在搜索引擎上找了很多中文的 ...

  5. Graph database_neo4j 底层存储结构分析(6)

    3.6  Node 数据存储 neo4j 中, Node 的存储是由 NodeStore 和 ArrayPropertyStore 2中类型配合来完成的. node 的label 内容是存在Array ...

  6. C#的async和await

    C# 5.0中引入了async 和 await.这两个关键字可以让你更方便的写出异步代码. 看个例子: public class MyClass { public MyClass() { Displa ...

  7. HighCharts入门

    一.什么是HighCharts 1.HighCharts是网页报表工具,开发语言是Javascript. 2.HighCharts是一个简单易用.美观.跨平台.跨浏览器的图表工具. 3.HighCha ...

  8. Leetcode | substr()

    求子串当然最经典的就是KMP算法了.brute force算法在leetcode上貌似也有一些技巧. brute force: char* StrStr(const char *str, const ...

  9. new NABCD

    小组名称: 天天向上 项目名称:连连看 小组成员:王森(组长).胡丽娜.林莉.张政.张金生 新NABCD模型 N(需求) 传统的连连看有许多,玩法单一,感觉没意思,用户更希望连连看游戏增加更多的与众不 ...

  10. javaWeb应用部署结构浅析

    要成功部署一个Web应用,则必须遵循以下标准(参考)目录结构. 2.目录说明 1)WEB-INF目录:必须直接放在Web应用上下文之下(即一级目录). 2)class目录:必须直接放在WEB-INF目 ...