Scramble String
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的更多相关文章
- 【leetcode】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- [LintCode] Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 45. Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- 【LeetCode练习题】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- 【一天一道LeetCode】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [leetcode]87. Scramble String字符串树形颠倒匹配
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- Leetcode:Scramble String 解题报告
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- leetcode87. Scramble String
leetcode87. Scramble String 题意: 给定一个字符串s1,我们可以通过将它分解为两个非空子字符串来表示为二叉树. 思路: 递归解法 对于每对s1,s2. 在s1某处切一刀,s ...
- [LeetCode] Scramble String -- 三维动态规划的范例
(Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...
- [leetcode] 87. Scramble String (Hard)
题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...
随机推荐
- 【java】关于时间
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created b ...
- [linux] Upgrading glibc for the GHOST Vulnerability
1> Test if the problem exists, code: #include <netdb.h> #include <stdio.h> #include & ...
- Linux变量
变量:(大的分为环境变量与本的变量) 本地变量: 本地变量在用户现在的shell生命期的脚本中使用.例如,本地变量file-name="loop.doc",这个值只在用户当前she ...
- Windows下面安装easy_install报UnicodeDecodeError: 'ascii' codec can't decode byte解决方法
在运行python ez_setup.py install后, 发现是在下载并解压setuptools-2.1,并运行setup.py时出现如下错误: 提示D:\Python27\lib\mimety ...
- Oracle数据库字段类型说明
目前Oracle 数据库大概有26个字段类型,大体分为六类,分别是字符串类型.数字数据类型.日期时间数据类型.大型对象(LOB)数据类型.RAW和LONG RAW数据类型.ROWID和UROWID数据 ...
- Android技能树
第一部分:Android(安卓)Android基础知识Android内存泄漏总结Handler内存泄漏分析及解决Android性能优化ListView详解RecyclerView和ListView的异 ...
- 转:已知2个整形数据a,b.不使用if,?:以及其他任何条件判断的语法,找出a跟b中数据的大者。
答案: int max(int a,int b){return (a+b+abs(a-b))/2;} 类似的 请定义一个宏,比较两个数a.b的大小,不能使用大于.小于.if语句 答案: #define ...
- iOS开发 火星坐标转百度坐标
CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(latitude, longitude);//原始坐标 //转换 google地图.s ...
- @Scheduled 注解
Spring配置文件xmlns加入 <!---加入:xmlns:task="http://www.springframework.org/schema/task"--> ...
- Centos6 使用yum安装 mysql 5.7
直接使用yum安装默认安装的是mysql 5.1版本,要想安装mysql 5.7 需要设置yum源 1.检查系统默认mysql,并删除 yum list installed | grep mysql ...