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. IE7下总提示" 缺少标识符、字符串或数字"

    用Jquery easyUI ,IE7下列表显示不了,总提示缺少标识符.字符串或数字.而google,maxthon,firefox,IE10等却没有问题. 原因是Json末尾多了个逗号.IE7下js ...

  2. php : mysql数据库操作类演示

    设计目标: 1,该类一实例化,就可以自动连接上mysql数据库: 2,该类可以单独去设定要使用的连接编码(set names XXX) 3,该类可以单独去设定要使用的数据库(use XXX): 4,可 ...

  3. vs2010打包(带数据库)图文详解

    最近刚刚打包发布了用VS2010开发的一个收费系统,借此讲一讲打包过程,供大家参考. 首先打开已经完成的工程,如图: 下面开始制作安装程序包. 第一步:[文件]——[新建]——[项目]——安装项目. ...

  4. TCP/IP协议学习(三) STM32中ETH驱动配置注意事项

    1.MII/RMII/SMI接口连接和配置 SMI又称站点管理接口,用于cpu与外置PHY芯片通讯,配置相关参数,包含MDC和MDIO两个管脚(CPU上有对应引脚,当然用普通GPIO口模拟SMI管理也 ...

  5. centos 安装redis自启动要点

    1.redis.conf a.daemonize yes b.pidfile /var/run/xxx.pid 2./etc/init.d/redis //加了下面三个注释部分,才支持设置开机自启动 ...

  6. 插件框架(Plugin Framework)

    HOW TO MAKE PLUGIN FRAMEWORK 插件系统构建 Dissect Eclipse Plugin Framework

  7. escape()、encodeURI()、encodeURIComponent()区别详解--zt

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  8. Jquery 中 ajaxSubmit使用讲解(转)

    1 ,引入依赖脚本 <script type="text/javascript" src="/js/jquery/jquery.form.js">& ...

  9. kvm虚拟机安装

    KVM虚拟化技术介绍 概述 KVM是基于内核的虚拟化技术(Kernel-based Virtual Machine),于2007年的Linux 2.6.20被合并进Linux内核.KVM要求CPU支持 ...

  10. touch id 开发

    min platform : 8.0 #import <LocalAuthentication/LocalAuthentication.h> LAContext *context = [[ ...