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. python处理.seq文件

    # Deal with .seq format for video sequence # Author: Kaij # The .seq file is combined with images, # ...

  2. rpm包制作

    ubuntu下先下载sudo apt-get install rpm就行了. 然后测试下rpm和rpmbuild命令都是存在的.好了,OK. rpm安装包的制作有严格的自定义的路径,这个路径是在/us ...

  3. UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>

    M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  4. Binary Tree Zigzag Level Order Traversal 解答

    Question Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, fro ...

  5. [置顶] 九度笔记之 1434:今年暑假不AC

    题目1434:今年暑假不AC 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:307 解决:180 题目描述: “今年暑假不AC?”“是的.”“那你干什么呢?”“看世界杯呀,笨蛋!”“@# ...

  6. More is better(并差集)

    More is better Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 327680/102400K (Java/Other) To ...

  7. 在Android中建立Android project没有R.java文件

    最近在搞一下安卓,在新建Android工程,既然发现在gen目录下没有R.java这个文件.我当时感到很郁闷,上次建Android工程才好好的,怎么这次既然报错没有R.java.后来我用以下才解决了. ...

  8. ASP.NET基于donetCHARTING的自动报表

    1,首先需要添加引用ChartExtents.dll和donetCHARTING.dll,资源百度大把. 2,配置图片生成类. using System; using System.Data; usi ...

  9. 设定MS SQL Server 2008定期自动备份

    1.说明 SQL Server2008 本身具有定期自动备份功能,我们只需要通过简单的配置就可以实现非常简单高效的自动备份功能. 2.打开SQL Server代理服务 要实现自动备份功能,首先要保证S ...

  10. weblogic配置jdbc数据源

    weblogic配置jdbc数据源的过程 方法/步骤   启动weblogic 管理服务器,使用管理用户登录weblogic管理控制台   打开管理控制台后,在左侧的树形域结构中,选择服务->数 ...