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 ...
随机推荐
- Hibernate 二级缓存 总结整理(转)
和<Hibernate 关系映射 收集.总结整理> 一样,本篇文章也是我很早之前收集.总结整理的,在此也发上来 希望对大家有用.因为是很早之前写的,不当之处请指正. 1.缓存:缓存是什么, ...
- <转>struts2中Convention中的basePackage与locators配置种种
用了Convention插件来实现所谓的0配置, 1. struts.convention.package.locators.basePackage=com.ZTest.web.action 这个属性 ...
- 在smarty模板中嵌入php代码
我个人并不太喜欢smarty的语法,写起来比较啰嗦易出现匹配出错,但是旧项目中有许多工程都是采用它作模板.最近需要在此上稍微加一些PHP的内容,但我不想在模板控制层去一个一个assign,而想在模板文 ...
- 看几道JQuery试题后总结(下篇)
感谢圆友的提醒 昨天下午完成了9道试题中的前4道,之后好多园友存在些疑惑和建议,在这里我一并说一下吧.首先对于昨天第一题可能存在误导,在JQuery中并没有innerHTML这个属性,不过我们可以将J ...
- Android Spinner(级联 天气预报)
activity_spinner.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...
- js 中中括号,大括号使用详解
一.{ } 大括号,表示定义一个对象,大部分情况下要有成对的属性和值,或是函数.如:var LangShen = {"Name":"Langshen",&quo ...
- Statement和PreparedStatement的特点 MySQL数据库分页 存取大对象 批处理 获取数据库主键值
1 Statement和PreparedStatement的特点 a)对于创建和删除表或数据库,我们可以使用executeUpdate(),该方法返回0,表示未影向表中任何记录 b)对于创建和 ...
- 1523. K-inversions(K逆序对)
1523 这题应该说有一些DP的思想吧 dp[i][j]表示以i为结尾第j个数的个数 k单调下降 直接求的话肯定超时 然后用树状数组来进行维护 求k-1次树状数组 #include <iostr ...
- poj3694Network(tarjan+lca)
http://poj.org/problem?id=3694 用tarjan算出桥,用lca算出公共祖先 把路上的边更新掉 原来的桥变为不是桥 看一解题报告感觉有一部分是不用加的 不知道是不是数据水 ...
- 【JAVA】别特注意,POI中getLastRowNum() 和getLastCellNum()的区别
hssfSheet.getLastRowNum();.行标,比行数小1 hssfSheet.getRow(k).getLastCellNum();//获取列数,比最后一列列标大1 行标.列标都以0开始 ...