[LeetCode] 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.
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(树的问题最易用递归)的更多相关文章
- Leetcode: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 -- 三维动态规划的范例
(Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...
- [LeetCode] 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 @ Python
原题地址:https://oj.leetcode.com/problems/scramble-string/ 题意: Given a string s1, we may represent it as ...
- [Leetcode] 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 乱串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [LeetCode] Scramble String 字符串 dp
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 【一天一道LeetCode】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【leetcode】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
随机推荐
- 三十分钟掌握STL
这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把它翻译出来.我没有对翻译出来的内容校验过.如果你没法在三十分钟内觉得有所收获,那么赶紧扔了 ...
- BZOJ1175 : [Balkan2007]The stairways of Saharna
杨氏图表,维护若干个单调不下降队列. 每次新加入一个数时,先考虑第一个队列: 如果可以放在最后,则放在最后. 否则找到最小的可以替换的替换掉,再将替换的数放入第二个队列,以此类推. 最后$ans_i= ...
- BZOJ3733 : [Pa2013]Iloczyn
首先将$n$的约数从小到大排序,设$dfs(x,y,z)$表示当前可以选第$x$个到第$m$个约数,还要选$y$个,之前选的乘积为$z$是否可能. 爆搜的时候,如果从$x$开始最小的$y$个相乘也超过 ...
- startInstrumentation asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
由于手头上一直没有android level 17及以上版本的手机,有一个shell命令启动脚本的BUG,发生在SDK level 17及以上 API>=17中加入了INTERACT_ACROS ...
- linux tar 备份命令
转载:http://www.douban.com/note/57861194/ tar [-cxtzjvfpPN] 文件与目录 ….参数:-c :建立一个压缩文件的参数指令(create 的意思):- ...
- YUV YCbCr
一,介绍 YUV是一种颜色空间 其中“Y”表示明亮度(Luminance或Luma),也就是灰阶值: 而“U”和“V” 表示的则是色度(Chrominance或Chroma),作用是描述影像色彩及饱和 ...
- C#设计模式——抽象工厂模式(原文转自:http://blog.jobbole.com/78059/)
一.引言 在上一专题中介绍了工厂方法模式,工厂方法模式是为了克服简单工厂模式的缺点而设计出来的,简单工厂模式的工厂类随着产品类的增加需要增加额外的代码),而工厂方法模式每个具体工厂类只完成单个实例的创 ...
- spark Mllib SVM实例
Mllib SVM实例 1.数据 数据格式为:标签, 特征1 特征2 特征3…… 0 128:51 129:159 130:253 131:159 132:50 155:48 156:238 157: ...
- Oracle用户信息查询
1.查看所有用户: select * from dba_users; select * from all_users; select * from user_users; 2.查看用户或角 ...
- Linux下LDAPSearch的例子
apt-get install ldap-utils LdapSearch examples The following examples are taking from various ldapse ...