【一天一道LeetCode】#87. Scramble String
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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 tTo 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 tWe 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 aWe 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.
(二)解题
题目大意:判断两个字符串是不是Scramble String,题目有点长,需要读好久!!!
举个简单的例子说明Scramble String,如ab和ba,那么ab拆分成a和b,ba拆分成b和a,这样就代表为Scramble String。
那么s1和s2是否为Scramble String,就需要将s1和s2拆分成两部分s11和s12,s21和s22,需要判断这两部分是否为Scramble String。
可以分成两个部分:(s11和s21,s12和s22)以及(s11和s22,s12和s21)。
这样一来代码就比较好写了!
很明显的动态规划问题。但是本人在做题过程中的优化还是没有做好,导致超时。
class Solution {
public:
bool isScramble(string s1, string s2) {
int len1 = s1.length();
int len2 = s2.length();
if (len1 != len2) return false;//长度不一样
if (s1 == s2) return true;//子串相等
//判断两个子串包含的字符是否相同
//最开始没加这一步,导致一直超时
vector<int> count(26,0);
for(int i = 0 ; i < len1 ; i++)
{
count[s1[i]-'a']++;
count[s2[i]-'a']--;
}
for(int i = 0 ; i < 26 ; i++)
{
if(count[i]!=0) return false;
}
//递归
for (int i = 1; i < len1; i++)
{
string subs1_1 = s1.substr(0, i);
string subs1_2 = s1.substr(i);
string subs2_1 = s2.substr(0, i);
string subs2_2 = s2.substr(i);
bool is1 = (subs1_1==subs2_1?true:isScramble(subs1_1, subs2_1)) && (subs1_2==subs2_2?true:isScramble(subs1_2, subs2_2));//这里也对递归进行了优化,相等则不进行递归
subs2_1 = s2.substr(len2-i, i);
subs2_2 = s2.substr(0,len2-i);
bool is2 = (subs1_1==subs2_1?true:isScramble(subs1_1, subs2_1)) && (subs1_2==subs2_2?true:isScramble(subs1_2, subs2_2));
if (is1 || is2) return true;//两部分任一部分为Scramble String则s1和s2就为Scramble String
}
return false;
}
};
【一天一道LeetCode】#87. Scramble String的更多相关文章
- [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 搅乱字符串
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
原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...
- 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 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】#8. String to Integer (atoi)
一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- 【leetcode】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
随机推荐
- 好用的jquery.animateNumber.js数字动画插件
在做公司的运营报告页面时,有一个数字累计增加的动画效果,一开始,毫无头绪,不知如何下手,于是上网查资料,发现大多都是用的插件来实现的,那么今天,我也来用插件jquery.animateNumber.j ...
- 两个对象用equals方法比较为true,它们的Hashcode值相同吗?
两个对象用equals方法比较为true,它们的Hashcode值相同吗? 答:不一定相同.正常情况下,因为equals()方法比较的就是对象在内存中的值,如果值相同,那么Hashcode值也应该相同 ...
- 用js实现排列组合
在leetcode上看到一个题,代码实现排列组合的. 记得大学上课时候,就用c写过,现在用js试试,顺便看看耗时. 先看看3的阶乘: function permute(temArr,testArr){ ...
- Java Socket通信代码片
package zhang; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOExcept ...
- python笔记十四(高阶函数——map/reduce、filter、sorted)
一.map/reduce 1.map() map(f,iterable),将一个iterable对象一次作用于函数f,并返回一个迭代器. >>> def f(x): #定义一个函数 ...
- windows下 gvim8.0 编译器配置
最近由于各种原因,IDE从source insight换成了vim,参考了诸多博客的文章,折腾了好久折腾了个大概的样子,现在总结一下经验: 主要参考: 改造vim变成source insight Wi ...
- 操作系统内核Hack:(四)内核雏形
操作系统内核Hack:(四)内核雏形 在本系列的前一篇文章<操作系统内核Hack:(三)BootLoader制作>中,我们制作出了一个两阶段引导BootLoader,并进入了一个内核的空壳 ...
- 【Java集合系列】---总体框架
个的组合,这些数据项可能共享某些特征,需要以某种操作方式一起进行操作,一般来说,这些数据项的类型都是相同的,或者基类相同(若使用的语言支持继承),列表或数组通常不认为是集合,因为其大小固定,但是事实上 ...
- android studio 转为eclipse快捷键后还存在的问题汇总
提取局部变量:Ctrl+Alt+V 提取全局变量:Ctrl+Alt+F 提取方法:Shit+Alt+M 使用android studio 出现红色下划线代表有错误产生,eclipse中的Ctrl+1( ...
- django-redis
linuxapt-get install redis-serverpip install django-redis vim /etc/redis/redis.conf maxmemory 20mb s ...