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 ...
随机推荐
- KafkaSpout分析:配置
public KafkaSpout(SpoutConfig spoutConf) { _spoutConfig = spoutConf;} 基于0.93版本的Storm SpoutConfig继承自K ...
- 几款开源的图形界面库(GUI Libraries)
SmartWin++ 遵循BSD许可协议的C++ GUI库,建立在Windows API之上,但仍可以通过使用WineLib在Linux/xNix上使用.也支持Pocket PC和基于Windows ...
- 李洪强iOS开发之宏定义方法来初始化一个单例对象
单例的使用: .m 为了方便实用,只要将以下代码定义在header文件或者.pch文件即可: // .h #define singleton_interface(class) + (instancet ...
- cocos2dx Sprite setBlendFunc 使用颜色混合:加算,减算
说明: 图片的混色是游戏的特效的特破口,应用的好,可以大量减少图片的使用量,有专门的介绍资料... setBlendFunc 相关参数,有多种组合方式,需要经验的积累 http://blog.csd ...
- Java经典书籍
Java Web开发教程---孙霞JSP应用开发详解(第三版)---刘晓华.张健.周慧贞Spring in Action---Craig Walls精通Struts基于MVC的Java Web设计与开 ...
- 几种必知的oracle结构图
一.数据库结构 二.Oracle 内存结构 三.进程结构 1. 用户进程:在数据库用户请求连接到Oracle 服务器时启动 2. 服务器进程:可以连接到Oracle实例,它在用户建立会话时启动 3. ...
- 深入理解Java虚拟机 - 虚拟机内存划分
在内存管理方面,Java相对于C和C++的区别在于Java具有内存动态分配以及垃圾收集技术,但平时我们很少去关注JVM的内存结构以及GC,在出现内存泄露或溢出方面的问题,排查工作将变得异常艰难. ...
- js不能执行,IE处理方法
一.如果你的ie不能打开js脚本(连系统里所有的js文件都不运行,网页上的js广告或好多页面都显示不了),请按一下步骤进行排查与解决: 1.查看是否IE的安全里面禁止了JS的运行: 将工具=>i ...
- UVa 1642 (综合) Magical GCD
题意: 给出一个数列,求一个连续的子序列,使得MGCD(i, j) = 该子序列的长度(j-i+1) × 子序列的gcd 最大,并输出这个最大值. 分析: 感觉可能要用优先队列,但貌似也用不上. 但 ...
- 修改Android默认背光值
/********************************************************************* * 修改Android默认背光值 * 说明: * 本文主要 ...