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字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...
随机推荐
- javascript guid(uuid)
http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript function generateU ...
- Response.BinaryWrite()方法输出二进制图像
protected void Page_Load(object sender, EventArgs e) { FileStream fs = new FileStream(Server.MapPath ...
- epoll的内部实现 & 百万级别句柄监听 & lt和et模式非常好的解释
epoll是Linux高效网络的基础,比如event poll(例如nodejs),是使用libev,而libev的底层就是epoll(只不过不同的平台可能用epoll,可能用kqueue). epo ...
- 函数指针与指针函数以及typedef
c难于理解的是指针,其魅力之处也是指针,函数方法结构,化繁为简可以理解为:返回值 函数名(形参表),具体来说: 返回值:1.可以为空void 2.基本数据类型char short int long f ...
- MyBatis中#,$的用法区别
#可以防止SQL注入 $用在一些特殊的SQL语句例如 order by ${...} , desc/asc PS:大部分用#,一些特殊情况用$
- Java简介
命令行输入参数不是在代码中设置输入的,而是这样做:选中你java项目中的java---右击----Run As---Run Configurations---Arguments----填入参数(多个参 ...
- 学习PYTHON之路, DAY 2 - PYTHON 基础 2(基础数据类型)
一 字符串格式化输出 name = 'nikita' age = 18 print ("I'am %s, age is %d") % (name, age) PS: 字符串是 %s ...
- Android 系统ID介绍
Android上系统ID有很多,本文只介绍常用的ANDROID ID.DEVICE ID.IMEI/MEID.WIFI/BT ADDRESS等几个,本文介绍这些ID的数据格式.长度及一些基本知识. 一 ...
- PHP数组去重..............过滤字段
$test_data = M('hot'); //实例化数据表 $data = $test_data->Distinct(true)->field('descriprion')->o ...
- iOS开发UI篇—简单介绍静态单元格的使用
iOS开发UI篇—简单介绍静态单元格的使用 一.实现效果与说明 说明:观察上面的展示效果,可以发现整个界面是由一个tableview来展示的,上面的数据都是固定的,且几乎不会改变. 要完成上面的效果, ...