哎,难题又不会做,

思路没有什么难度,

关键是要把问题思考透彻,

要考虑的要点还是挺多的,不容易一下子都考虑到;

我开始的思路是按照树的根去递归的,

这样就必须要没有重复元素,因为需要每次在s2中查找s1中的元素;

怎么说呢,有点考虑的过于specific了,其实直接把字符串分成两部分递归就可以了,

不过需要注意不能漏掉两部分可能次序调换的情况。

 1 class Solution {
 2 public:
 3     bool isScramble(string s1, string s2) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         if (s1.empty() && s2.empty()) {
 7             return true;
 8         }
 9         if (s1 == s2) {
10             return true;
11         }
12         return scramble(s1, 0, s1.size() - 1, s2, 0, s2.size() - 1);
13     }
14     bool scramble(string &s1, int b1, int e1, string &s2, int b2, int e2) {
15         if (!same(s1, b1, e1, s2, b2, e2)) {
16             return false;
17         }
18         int len = e1 - b1 + 1;
19         if (len == 0) {
20             return true;
21         }
22         if (len == 1) {
23             return (s1[b1] == s2[b2]);
24         }
25         for (int i = 0; i < len - 1; ++i) {
26             if ((scramble(s1, b1, b1 + i, s2, b2, b2 + i) && scramble(s1, b1 + i + 1, e1, s2, b2 + i + 1, e2)) ||
27                 (scramble(s1, b1, b1 + i, s2, e2 - i, e2)) && scramble(s1, b1 + i + 1, e1, s2, b2, e2 - i - 1)) {
28                 return true;
29             }
30         }
31         return false;
32     }
33     bool same(string &s1, int b1, int e1, string &s2, int b2, int e2) {
34         vector<int> count(256, 0);
35         for (int i = b1; i <= e1; ++i) {
36             ++count[s1[i]];
37         }
38         for (int i = b2; i <= e2; ++i) {
39             --count[s2[i]];
40         }
41         for (int i = 0; i < 256; ++i) {
42             if (count[i] != 0) {
43                 return false;
44             }
45         }
46         return true;
47     }
48 };

转载于:https://www.cnblogs.com/chasuner/p/scramble.html

LeetCode-Scramble String相关推荐

  1. LeetCode Scramble String

    LeetCode解题之Scramble String 原题 一个字符串能够拆分成两个都不为空的子字符串,而子字符串(长度大于等于二)也能够不断这样拆分下去.如今能够随意交换拆分出来两部分的位置来改变字 ...

  2. 【To Understand !!! DP or 递归】LeetCode 87. Scramble String

    LeetCode 87. Scramble String 参考链接:[1]https://blog.csdn.net/makuiyu/article/details/44926439 [2]http: ...

  3. 【细节实现题】LeetCode 8. String to Integer (atoi)

    LeetCode 8. String to Integer (atoi) Solution1:我的答案 参考链接:http://www.cnblogs.com/grandyang/p/4125537. ...

  4. LeetCode 8. String to Integer (atoi)(字符串)

    LeetCode 8. String to Integer (atoi)(字符串) LeetCode 8 String to Integer atoi字符串 问题描述 解题思路 参考代码 By Sca ...

  5. Scramble String -- LeetCode

    原题链接: http://oj.leetcode.com/problems/scramble-string/  这道题看起来是比較复杂的,假设用brute force,每次做分割,然后递归求解,是一个 ...

  6. 【LeetCode】87. Scramble String

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

  7. [leetcode] 根据String数组构造TreeNode,用于LeetCode树结构相关的测试用例

    LeetCode 跟树结构相关的题目的测试用例中大多是通过String数组来构造树.例如{2,#,3,#,4,#,5,#,6},可以构造出如下的树(将树结构逆时针选择90度显示): 6        ...

  8. LeetCode Rearrange String k Distance Apart

    原题链接在这里:https://leetcode.com/problems/rearrange-string-k-distance-apart/description/ 题目: Given a non ...

  9. LeetCode Interleaving String

    原题链接在这里:https://leetcode.com/problems/interleaving-string/ 这是一道DP题,用boolean数组dp维护历史数据,dp[i][j]表示长度为i ...

  10. LeetCode 443 String Compression(双指针)

    问题:给出一个字符串,对其压缩.对于连续出现的字符,用字符+出现次数来表示.单个情况时,数字1可以省略. 思路:使用ancher来表示检查的起始位置,遍历时记录不相同的位置,此时作压缩替换. 具体代码 ...

最新文章

  1. AIProCon在线大会笔记之张钹院士:探索第三代人工智能,需要勇闯无人区的人才!
  2. 【C/C++9】天气APP:Oracle的虚表/日期/序列,索引/视图/链路/同义词,数据库高可用性
  3. System BIOS设置
  4. mysql-ubuntu卸载安装mysql
  5. 形容人的内核是什么意思_识人核心是什么?
  6. java每天定时任务
  7. PyTorch中“CUDA out of memory”的调试笔记
  8. 创建SpringBoot项目的两种姿势
  9. c# npoi 打开已经存在excel_用了这个jupyter插件,我已经半个月没打开过excel了
  10. 地磅15针到9针数据线连接串口数据采集方法
  11. 360随身wifi3代linux驱动下载,360随身wifi3驱动
  12. html如何添加时钟效果,HTML5实现时钟效果
  13. 项目干系人管理-知识领域
  14. Kubectl常用命令(三)
  15. 记响应式布局vh/vw单位在安卓端微信浏览器以及UC浏览器的坑
  16. IntelliJ IDEA)中弹出“IntelliJ IDEA License Activation”时怎么办
  17. 算法学习笔记----用动态规划解决钢管切割问题
  18. 指针数组和数组指针有什么区别?
  19. Java实现一个GUI的SEO软文生成器v1
  20. 中国系统网 Ghost Win8(32) 纯净 装机版

热门文章

  1. 28 | 读写分离有哪些坑?
  2. LeetCode(81): 搜索旋转排序数组 II
  3. jdk1.7 String switch的实现
  4. Selleck --- 01Cookie
  5. STL学习笔记(仿函数)
  6. windows下面虚拟主机
  7. Unable to delete directory: D:\Downloads\githubdownfive\tianxmyapp\library\
  8. table 权限 展示页面
  9. javascript event
  10. mysql(2)—— 由笛卡尔积现象分析数据库表的连接