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.

     bool isScramble(string s1, string s2) {
string comp1=s1,comp2=s2;
sort(comp1.begin(),comp1.end());
sort(comp2.begin(),comp2.end());
if(comp1!=comp2)
return false;
if(comp1.length()==)
return true;
int len=s1.length();
int i;
for(i=;i<len;i++)
{
string s1_left=s1.substr(,i);
string s1_right=s1.substr(i);
string s2_left=s2.substr(,i);
string s2_right=s2.substr(i);
if(isScramble(s1_left,s2_left)&&isScramble(s1_right,s2_right))
return true;
s2_left=s2.substr(len-i);
s2_right=s2.substr(,len-i);
if(isScramble(s1_left,s2_left)&&isScramble(s1_right,s2_right))
return true;
}
return false; }

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. [LintCode] Scramble String 爬行字符串

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

  3. 45. Scramble String

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

  4. 【LeetCode练习题】Scramble String

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

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

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

  6. [leetcode]87. 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 解题报告

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

  8. leetcode87. Scramble String

    leetcode87. Scramble String 题意: 给定一个字符串s1,我们可以通过将它分解为两个非空子字符串来表示为二叉树. 思路: 递归解法 对于每对s1,s2. 在s1某处切一刀,s ...

  9. [LeetCode] Scramble String -- 三维动态规划的范例

    (Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...

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

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

随机推荐

  1. mysql跨数据库操作问题

    跨数据库的操作,如果是命名符合mysql规范的直接database.table,如果不符合规范比如加了 -  等符号需要在数据库上面加`database`.table(不是'database'.tab ...

  2. ionic cordova

    ionic cordova 打包 文章 http://blog.csdn.net/i348018533/article/details/47258449 http://www.cnblogs.com/ ...

  3. git: windows git ssh keys生成

    http://blog.csdn.net/lsyz0021/article/details/52064829 当我们使用github或者bitbucket等仓库时我们有可能需要ssh认证,所以需要生成 ...

  4. 【安全测试】burpsuite安装方法

    burp suite需要安装Java环境才可以运行,最好安装jdk1.6以上版本. 1.将jdk安装路径添加到环境变量-path里,加到bin即可: C:\Program Files\Java\jdk ...

  5. EaseType缓动函数

    http://sol.gfxile.net/interpolation/   一篇很详细的图文

  6. mybitis学习的页面

    http://mybatis.github.io/mybatis-3/zh/configuration.html

  7. JS转换时间戳为“刚刚”、“1分钟前”、“2小时前”“1天前”等格式

    var minute = 1000 * 60; var hour = minute *60; var day = hour *24; var week = day * 7; var month = d ...

  8. C语言文法的理解

    <程序> ->  <外部声明> |  <程序>  <外部声明> <外部声明>  ->   <函数定义>  |  &l ...

  9. Create Linked Server SQL Server 2008

    From:http://www.jensbits.com/2010/11/10/create-linked-server-sql-server-2008/ http://www.c-sharpcorn ...

  10. Servlet里面url-pattern的通配符*的使用规则

    简单来说: 以”/’开头和以”/*”结尾的是用来做路径映射的. 以前缀”*.”开头的是用来做扩展映射的. “/” 是用来定义default servlet映射的. 剩下的都是用来定义详细映射的.比如: ...