leetcode87. Scramble String
leetcode87. Scramble String
题意:
给定一个字符串s1,我们可以通过将它分解为两个非空子字符串来表示为二叉树。
思路:
递归解法
对于每对s1,s2. 在s1某处切一刀,s1分成left,right,然后在s2首部开始等长的地方切一刀,切成left,right.只要s1的left和s2的left,s1的right和s2的right同样也构成scramble string就可以了。 递归解法需要剪枝。
- 剪枝1:s1,s2不等长return
- 剪枝2:s1,s2字符不相同return
动态规划
这题动态规划解法时间不如递归的,但是这题用动态规划还是比较牛皮的。map[i][j][k]三维数组储存,i,j分别表示s1,s2开始的下标,k表示i,j开始后组成的长度。虽然说看到两个字符串比较感觉像动态规划。但是不好想呀。
参考博文
ac代码:
C++ (递归)
class Solution {
public:
bool isScramble(string s1, string s2) {
int len1 = s1.length();
int len2 = s2.length();
if(len1 != len2) return false;
if(len1 == 1) return s1[0] == s2[0];
int cnt[256] = {0};
for(int i = 0; i < len1; i++) cnt[s1[i]]++;
for(int j = 0; j < len2; j++) cnt[s2[j]]--;
for(int i = 0; i < 256; i++)
if(cnt[i]) return false;
for(int i = 0; i < len1 - 1; i++)
{
if(isScramble(s1.substr(0,i + 1),s2.substr(0,i + 1)) && isScramble(s1.substr(i + 1),s2.substr(i + 1)) ||
isScramble(s1.substr(0,i + 1),s2.substr(len2 - 1 - i, i + 1)) && isScramble(s1.substr(i + 1),s2.substr(0,len2 - 1 - i)))
return true;
}
return false;
}
};
C++ (动态规划)
class Solution {
public:
bool isScramble(string s1, string s2) {
int len1 = s1.length();
int len2 = s2.length();
if(len1 != len2) return false;
if(!len1) return true;
vector<vector<vector<bool> > >map(len1, vector<vector<bool> >(len1, vector<bool>(len1, false)));
for(int j = 0; j < len1; j++)
for(int k = 0; k < len1; k++)
if(s1[j] == s2[k]) map[0][j][k] = true;
for(int i = 1; i < len1; i++)
{
for(int j = len1 - 1; j >= 0; j--)
{
for(int k = len1 - 1; k >= 0; k--)
{
if(k + i + 1 > len1 || j + i + 1 > len1) continue;
//map[i][j][k] = false;
for(int u = 0; u < i; u++)
{
if((map[u][j][k + i - u] && map[i - u - 1][j + u + 1][k]) ||
(map[u][j][k] && map[i - u - 1][j + u + 1][k + u + 1]))
{
map[i][j][k] = true;
break;
}
}
}
}
}
return map[len1 - 1][0][0];
}
};
leetcode87. Scramble String的更多相关文章
- 【leetcode】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- [LintCode] Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 45. Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- 【LeetCode练习题】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- 【一天一道LeetCode】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [leetcode]87. Scramble String字符串树形颠倒匹配
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- Leetcode:Scramble String 解题报告
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- [LeetCode] Scramble String -- 三维动态规划的范例
(Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...
- [leetcode] 87. Scramble String (Hard)
题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...
随机推荐
- 如何将vmworkstation的虚机导成ovf模版
如何将vmworkstation的虚机导成ovf模版 最近碰见一个事情挺烦的苦恼了我好长一段时间,是这样的公司要进行攻防演练需要搭建一个owaps的靶站练手,环境我在我的电脑上已经搭好了(vmwork ...
- 健身VS不健身,完全是两种不同的人生!
这两天一组同龄人合照 刷爆了国内健身圈, 图左是一位67岁的老人, 图右是67岁的健美运动员杨新民老师 相同年龄, 但从外观上有着强烈的距离感! 让多人不禁感叹,健身和不健身, 简直就是两种状态,两种 ...
- React 16 源码瞎几把解读 【前戏】 为啥组件外面非得包个标签?
〇.看前准备 1.自行clone react最新代码 2.自行搭建一个能跑react的test项目 一.看表面:那些插件 如何解析JSX 有如下一段代码: // ---- hearder.jsx 组件 ...
- HDU 6194 string string string 2017沈阳网络赛 后缀数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6194 题意:告诉你一个字符串和k , 求这个字符串中有多少不同的子串恰好出现了k 次. 解法:后缀数组 ...
- linux系统定时任务设置
.使用at命令设置一次性定时任务 2.使用crontab设置周期性定时任务 1)cd /home 目录下,使用vi test.py创建文件,内容如下: #!/usr/bin/python#coding ...
- docker swarm join 报错
[peter@minion ~]$ docker swarm join --token SWMTKN-1-3mj5po3c7o04le7quhkdhz6pm9b8ziv3qe0u7hx0hrgxsna ...
- python_线程、进程和协程
线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. #!/usr/bin/env python #coding=utf-8 __author__ = 'yinjia' i ...
- POJ 1661 Help Jimmy(二维DP)
题目链接:http://poj.org/problem?id=1661 题目大意: 如图包括多个长度和高度各不相同的平台.地面是最低的平台,高度为零,长度无限. Jimmy老鼠在时刻0从高于所有平台的 ...
- java-list-分组
Map<String, List<Hb12Domain>> groupBy = hb18DomainList.stream().collect(Collectors.group ...
- Rule Compilation error xxx cannot be resolved
说明:在eclipse中部署服务器时不报错,但在dos窗口中部署时报如下异常 原因:规则引擎drl文件文件中两个含义相同的变量的中文注释(只能用//,不能使用/**xxx*/或/*xxx*/)要保持相 ...