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 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.
class Solution {
public:
bool check(string s1, string s2) {
if(s1.length() != s2.length()) return false; string cp1 = s1, cp2 = s2;
sort(cp1.begin(), cp1.end());
sort(cp2.begin(), cp2.end());
for(int i=; i<cp1.length(); ++i) {
if(cp1[i] != cp2[i]) return false;
}
return true;
}
bool dfs(string s1, string s2) {
int m = s1.length(), n = s2.length();
if(!check(s1, s2)) return false;
if(m == ) {
if(s1 == s2) return true;
return false;
} string l, r, p, q;
for(int le = ; le < m; ++le) {
l = s1.substr(, le);
r = s1.substr(le, m - le);
p = s2.substr(, le);
q = s2.substr(le, m - le);
if(dfs(l, p) && dfs(r, q)) return true;
else {
p = s2.substr(m - le, le);
q = s2.substr(, m - le);
if(dfs(l, p) && dfs(r, q)) return true;
}
} return false;
} bool isScramble(string s1, string s2) {
int m = s1.length(), n = s2.length(); if(m != n) return false; return dfs(s1, s2);
}
};
leetcode@ [87] Scramble String (Dynamic Programming)的更多相关文章
- [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] 87. Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [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] 87. Scramble String (Hard)
题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...
- Leetcode#87 Scramble String
原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...
- 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】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 动态规划 日期 题目地址:https://le ...
随机推荐
- Unity3d大会的部分总结
原地址:http://blog.csdn.net/sgnyyy/article/details/23775219 一.项目开发,管理和发布策略 1. 四大准则 a. 美术的资源 ...
- Unity3D开发之查找面板上某个脚本(包括Missing)
原地址:http://blog.csdn.net/lihandsome/article/details/24265411 有时候我们需要知道某个脚本在场景上面哪里用到,或者那个脚本被删除了但又没有把相 ...
- -webkit-text-size-adjust: none;该如何处理
-webkit-text-size-adjust: none; 在中文版Chrome里面,网页CSS里所有小于12px的字体设置都无效,最终将显示12px.这样弄的本意可能 是好的,因为中文一旦小于1 ...
- linux fork函数与vfork函数
一.fork1. 调用方法#include <sys/types.h>#include <unistd.h> pid_t fork(void);正确返回:在父进程中返回子进程的 ...
- linq集合内部赋值
linq集合内部赋值 比如将一个列的值,赋值给另一列 有三种方法: 1. e.Result.ToList().ForEach(n => n.IsIntermediarybool = SetIsI ...
- 2015-J. PUMA
描述 (题名来源:2009年校队出战合肥现场赛曾用队名) PUMA是全球著名的运动品牌,PUMA的鞋与服饰在嘻哈涂鸦文化中受到全球各地年轻人的极度欢迎,同时PUMA与adidas更是1970与1980 ...
- HDU1565+状态压缩dp
简单的压缩状态 dp /* 状态压缩dp 同hdu2167 利用滚动数组!! */ #include<stdio.h> #include<string.h> #include& ...
- 分布式设计与开发(三)------高一致性服务ZooKeeper
分布式环境中大多数服务是允许部分失败,也允许数据不一致,但有些最基础的服务是需要高可靠性,高一致性的,这些服务是其他分布式服务运转的基础,比如naming service.分布式lock等,这些分布式 ...
- Trainning Guide, Data Structures, Example
最近在复习数据结构,发现这套题不错,题目质量好,覆盖广,Data Structures部分包括Example,以及简单,中等,难三个部分,这几天把Example的做完了, 摘要如下: 通过这几题让我复 ...
- Android studio中不同颜色代表什么意思
和你的版本控制工具相关 绿色,已经加入控制暂未提交红色,未加入版本控制蓝色,加入,已提交,有改动白色,加入,已提交,无改动