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":

  1. great
  2. / \
  3. gr eat
  4. / \ / \
  5. g r e at
  6. / \
  7. 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".

  1. rgeat
  2. / \
  3. rg eat
  4. / \ / \
  5. r g e at
  6. / \
  7. 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".

  1. rgtae
  2. / \
  3. rg tae
  4. / \ / \
  5. r g ta e
  6. / \
  7. 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()。

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

代码如下:

  1. class Solution {
  2. public:
  3. bool isScramble(string s1, string s2) {
  4. string sort1 = s1,sort2 = s2;
  5. sort(sort1.begin(),sort1.end());
  6. sort(sort2.begin(),sort2.end());
  7. if(sort1.compare(sort2) != )
  8. return false;
  9. if(s1.compare(s2) == )
  10. return true;
  11.  
  12. int len = s1.size();
  13. for(int i = ; i < len; i++){
  14. string s11 = s1.substr(,i);
  15. string s12 = s1.substr(i);
  16. string s21 = s2.substr(,i);
  17. string s22 = s2.substr(i);
  18.  
  19. if(isScramble(s11,s21)&&isScramble(s12,s22))
  20. return true;
  21. s21 = s2.substr(,len-i);
  22. s22 = s2.substr(len-i);
  23. if(isScramble(s11,s22)&&isScramble(s12,s21))
  24. return true;
  25. }
  26. return false;
  27. }
  28. };

【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. Android Project from Existing Code 生成 R 文件错误、失败等问题解决办法 - 持续更新

    Android Project from Existing Code 生成 R 文件错误.失败等问题解决办法 - 持续更新 git  上的项目,pull下来之后用Android Project fro ...

  2. tomcat使用说明

    tomcat使用说明   1.tomcat的目录结构及说明: 2.发布WEB应用 1).将应用目录达成war包.(将html.jsp.images.WEB-INF目录和当前目录下的所有后缀名为jsp. ...

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

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

  4. RMAN数据库恢复之恢复表空间和数据文件

    执行表空间或数据文件恢复时,数据库既可以是MOUNT状态,也可以是OPEN状态.1.恢复表空间在执行恢复之前,如果被操作的表空间未处理OFFLINE状态,必须首先通过ALTER TABLESPACE… ...

  5. [网络流最大流经典][uva 11082][矩阵解压]

    题目大意 分析 #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring ...

  6. 【筛素数表证明】【O[n]】

    void get_prime() { int cnt = 0; for (int i = 2; i < N; i++) { if (!tag[i]) p[cnt++] = i; for (int ...

  7. apache 日志中记录代理IP以及真实客户端IP

    vim /usr/local/apach2/conf/httpd.conf 默认情况下log日志格式为:LogFormat "%h %l %u %t \"%r\" %&g ...

  8. java中List的用法

    list的添加删除等操作 import java.util.*; class TestList { public static void main(String[] args) { List<S ...

  9. phantomjs API

    phantomjs使用说明         phantomjs实现了一个无界面的webkit浏览器.虽然没有界面,但dom渲染.js运行.网络访问.canvas/svg绘制等功能都很完备,在页面抓取. ...

  10. ubuntu python PyCharm virtualenv

    1.安装virtualenv 参照:http://docs.jinkan.org/docs/flask/installation.html 2.PyCharm结合virtualenv开发应用 PyCh ...