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.

解题思路:

要满足isScramble(string s1,string s2),则必然满足isScramble(s11,s21)&&isScramble(s12,s22)或者isScramble(s11,s22)&&isScramble(s12,s21)

递归结束条件,当s1.compare(s2) == 0 时return false,即s1 和 s2 都只有一个字符且相等的时候。

当排序的sort1 ,sort2 不相等,即说明s1 和 s2 中的字符不同,return false,加上这个检查就可以大大的减少递归次数。否则就会超时。

每一次调用的s1 和 s2 的长度都是相等的,所以isScramble(s11,s21)&&isScramble(s12,s22)的时候s11.size() == s21.size(),

isScramble(s11,s22)&&isScramble(s12,s21)的时候s11.size() == s22.size()。

还有动态规划的解法,目前还不太熟,正在研究中……

代码如下:

class Solution {
public:
bool isScramble(string s1, string s2) {
string sort1 = s1,sort2 = s2;
sort(sort1.begin(),sort1.end());
sort(sort2.begin(),sort2.end());
if(sort1.compare(sort2) != )
return false;
if(s1.compare(s2) == )
return true; int len = s1.size();
for(int i = ; i < len; i++){
string s11 = s1.substr(,i);
string s12 = s1.substr(i);
string s21 = s2.substr(,i);
string s22 = s2.substr(i); if(isScramble(s11,s21)&&isScramble(s12,s22))
return true;
s21 = s2.substr(,len-i);
s22 = s2.substr(len-i);
if(isScramble(s11,s22)&&isScramble(s12,s21))
return true;
}
return false;
}
};

【LeetCode练习题】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. [leetcode]87. Scramble String字符串树形颠倒匹配

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

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

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

  4. [LeetCode] 87. Scramble String 搅乱字符串

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

  5. [LeetCode] 87. Scramble String 爬行字符串

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

  6. 【leetcode】 Scramble String (hard)★

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

  7. Leetcode#87 Scramble String

    原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...

  8. leetcode@ [87] Scramble String (Dynamic Programming)

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

  9. leetcode[86] Scramble String

    将一个单词按照这种方式分: Below is one possible representation of s1 = "great": great / \ gr eat / \ / ...

  10. leetCode 87.Scramble String (拼凑字符串) 解题思路和方法

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

随机推荐

  1. Eclipse总是自动关闭

    -Dosgi.requiredJavaVersion=1.5把INI文件中的这一行删除掉,貌似这样以后模拟器也没再出现timeout的问题了,O(∩_∩)O哈哈~

  2. web前端设计:JQuery MINI UI

    JQuery MINIUI 个人感觉用起来很爽,所以在此记录之,以后开发过程可能作为备选项.它能缩短开发时间,减少代码量,使开发者更专注于业务和服务端,轻松实现界面开发,带来绝佳的用户体验.在线下载地 ...

  3. python刷取CSDN博文访问量之三

    python刷取CSDN博文访问量之三   作者:vpoet 注:这个系列我只贴代码,代码不注释.有兴趣的自己读读就懂了,纯属娱乐,望管理员抬手若有转载一定不要注明来源 #coding=utf-8 i ...

  4. Volist标签 key值的使用

    Volist标签是thinkphp框架view中常用到的一个标签.主要用于在模板中循环输出数据集或者多维数组.今天使用key属性时出了点问题,记录一下. volist标签(循环输出数据) 闭合 非闭合 ...

  5. 【bzoj1031】[JSOI2007]字符加密Cipher

    题目描述 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法:把需要加密的信息排成一圈,显然,它们有很多种不同的读法.例如下图,可以读作:JSOI07 ...

  6. 怎样查询SCI和EI检索号

    为了年终考核,花了一个早上才搞清楚,里面有非常多小问题.以下具体说明具体过程: SCI检索号 1.进入图书馆主页: 2.选择"电子数据库": 3.选择外文数据库中的"We ...

  7. poj1664 放苹果(递归)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1664 ------ ...

  8. STL源代码剖析 读书总结

    <<STL源代码剖析>> 侯捷著 非常早就买了这本书, 一直没看, 如今在实验室师兄代码的时候发现里面使用了大量泛型编程的内容, 让我有了先看看这本书的想法. 看之前我对于泛型 ...

  9. 由闭包引起的对javascript代码可维护性的思考

    在最近的编程实践中由闭包的使用引起了我对javascript代码可维护性的思考.面向对象的其中一个特性封装性通过封装可以降低类与类之间或模块与模块之间耦合性从而使我们的设计更加高内聚低耦合,在大规模的 ...

  10. C# LiNq的语法以及常用的扩展方法

    首先先来扯一下,这篇博文是我第一次写的,主要是我的一些摘录,希望对大家有所帮助. Linq的基础 •LINQ(读音link):Linq To SQL(过时).Linq To Object.Linq T ...