LeetCode-Scramble String
哎,难题又不会做,
思路没有什么难度,
关键是要把问题思考透彻,
要考虑的要点还是挺多的,不容易一下子都考虑到;
我开始的思路是按照树的根去递归的,
这样就必须要没有重复元素,因为需要每次在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相关推荐
- LeetCode Scramble String
LeetCode解题之Scramble String 原题 一个字符串能够拆分成两个都不为空的子字符串,而子字符串(长度大于等于二)也能够不断这样拆分下去.如今能够随意交换拆分出来两部分的位置来改变字 ...
- 【To Understand !!! DP or 递归】LeetCode 87. Scramble String
LeetCode 87. Scramble String 参考链接:[1]https://blog.csdn.net/makuiyu/article/details/44926439 [2]http: ...
- 【细节实现题】LeetCode 8. String to Integer (atoi)
LeetCode 8. String to Integer (atoi) Solution1:我的答案 参考链接:http://www.cnblogs.com/grandyang/p/4125537. ...
- LeetCode 8. String to Integer (atoi)(字符串)
LeetCode 8. String to Integer (atoi)(字符串) LeetCode 8 String to Integer atoi字符串 问题描述 解题思路 参考代码 By Sca ...
- Scramble String -- LeetCode
原题链接: http://oj.leetcode.com/problems/scramble-string/ 这道题看起来是比較复杂的,假设用brute force,每次做分割,然后递归求解,是一个 ...
- 【LeetCode】87. Scramble String
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- [leetcode] 根据String数组构造TreeNode,用于LeetCode树结构相关的测试用例
LeetCode 跟树结构相关的题目的测试用例中大多是通过String数组来构造树.例如{2,#,3,#,4,#,5,#,6},可以构造出如下的树(将树结构逆时针选择90度显示): 6 ...
- LeetCode Rearrange String k Distance Apart
原题链接在这里:https://leetcode.com/problems/rearrange-string-k-distance-apart/description/ 题目: Given a non ...
- LeetCode Interleaving String
原题链接在这里:https://leetcode.com/problems/interleaving-string/ 这是一道DP题,用boolean数组dp维护历史数据,dp[i][j]表示长度为i ...
- LeetCode 443 String Compression(双指针)
问题:给出一个字符串,对其压缩.对于连续出现的字符,用字符+出现次数来表示.单个情况时,数字1可以省略. 思路:使用ancher来表示检查的起始位置,遍历时记录不相同的位置,此时作压缩替换. 具体代码 ...
最新文章
- AIProCon在线大会笔记之张钹院士:探索第三代人工智能,需要勇闯无人区的人才!
- 【C/C++9】天气APP:Oracle的虚表/日期/序列,索引/视图/链路/同义词,数据库高可用性
- System BIOS设置
- mysql-ubuntu卸载安装mysql
- 形容人的内核是什么意思_识人核心是什么?
- java每天定时任务
- PyTorch中“CUDA out of memory”的调试笔记
- 创建SpringBoot项目的两种姿势
- c# npoi 打开已经存在excel_用了这个jupyter插件,我已经半个月没打开过excel了
- 地磅15针到9针数据线连接串口数据采集方法
- 360随身wifi3代linux驱动下载,360随身wifi3驱动
- html如何添加时钟效果,HTML5实现时钟效果
- 项目干系人管理-知识领域
- Kubectl常用命令(三)
- 记响应式布局vh/vw单位在安卓端微信浏览器以及UC浏览器的坑
- IntelliJ IDEA)中弹出“IntelliJ IDEA License Activation”时怎么办
- 算法学习笔记----用动态规划解决钢管切割问题
- 指针数组和数组指针有什么区别?
- Java实现一个GUI的SEO软文生成器v1
- 中国系统网 Ghost Win8(32) 纯净 装机版