【leetcode】Scramble String
Scramble String
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.
2. 如果两个substring中间某一个点,左边的substrings为scramble string, 同时右边的substrings也为scramble string,则为true
3. 如果两个substring中间某一个点,s1左边的substring和s2右边的substring为scramble string, 同时s1右边substring和s2左边的substring也为scramble string,则为true
class Solution {
public:
bool isScramble(string s1, string s2) { int n=s1.length();
vector<vector<vector<bool>>> dp(n,vector<vector<bool>>(n,vector<bool>(n+)));
//dp[i][j][k] represent whether s1[i,i+1,...,i+k-1] and s2[j,j+1,...,j+k-1] is scramble for(int i=n-;i>=;i--)
{
for(int j=n-;j>=;j--)
{
for(int k=;k<=n-max(i,j);k++)
{
if(s1.substr(i,k)==s2.substr(j,k))
{
dp[i][j][k]=true;
}
else
{
for(int l=;l<k;l++)
{
if(dp[i][j][l]&&dp[i+l][j+l][k-l]||dp[i][j+k-l][l]&&dp[i+l][j][k-l])
{
dp[i][j][k]=true;
break;
}
} }
}
} }
return dp[][][n];
}
};
【leetcode】Scramble String的更多相关文章
- 【leetcode】 Scramble String (hard)★
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【LeetCode】984. String Without AAA or BBB 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串构造 日期 题目地址:https://leet ...
- 【leetcode】 Interleaving String (hard)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【leetcode】8. String to Integer (atoi)
题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
- 【leetcode】443. String Compression
problem 443. String Compression Input ["a","a","b","b"," ...
随机推荐
- 跨区域的application共享问题。
@1 new Thread(){ @Override public void run() { getConnect(); } }.start(); 如果我们的一个的应用程序有俩个入口.那么如果我们在这 ...
- “Transaction rolled back because it has been marked as rollback-only”
spring的声明事务提供了强大功能,让我们把业务关注和非业务关注的东西又分离开了.好东西的使用,总是需要有代价的.使用声明事务的时候,一 个不小心经常会碰到“Transaction rolled b ...
- eclipse的安装环境及eclipse下maven的配置安装
之前安装zookeeper的时候,就配置过linux下的java环境,即安装过linux JDK,配置过JAVA_HOME 和PATH 变量,,, 现在要运行一个java客户端,来消费kafka ...
- highstock 的tooltip框里面的内容 保留两位小数的办法
$("#flux_chart_container").highcharts(' }, borderWidt ...
- Properties类的使用方法
它提供了几个主要的方法: 1. getProperty ( String key),用指定的键在此属性列表中搜索属性.也就是通过参数 key ,得到 key 所对应的 value. 2. load ( ...
- 国内SEO如何过滤掉不良网络信息
对于站长们来说,首要任务就是和搜索引擎战斗,面对搜索引擎算法的不断更新,站长们也更加头疼.站长们都觉得,搜索引擎才是网站优化的"统治者",和谷歌优化相比,中国的SEO优化要复杂的多 ...
- Javascript软键盘设计
国内大多数网站的密码在网络传输过程中都是明文的,我们目前正在做的产品也是这样的情形,这正常吗? 大家都偷懒?不重视安全?各人持有观点,有人认为明文传输并不是想象中的那么可怕,事实上正常情况下这些报文你 ...
- mybatis 多个dao重名,根据namespace解析
在mybatis通过执行sql语句的方式是,用getSqlSession().xxx(param,..)方法来调用, 其中第一个参数就是dao mapper.xml文件的命名空间.id package ...
- static 的使用
static用法小结 转自 http://blog.csdn.net/Kendiv/article/details/675941 在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有 ...
- CSU 1113 Updating a Dictionary(map容器应用)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为 ...