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.

 
 
1. 如果两个substring相等的话,则为true
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的更多相关文章

  1. 【leetcode】 Scramble String (hard)★

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  2. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  3. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  4. 【LeetCode】984. String Without AAA or BBB 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串构造 日期 题目地址:https://leet ...

  5. 【leetcode】 Interleaving String (hard)

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  6. 【leetcode】Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  7. 【LeetCode】8. String to Integer (atoi) 字符串转整数

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  8. 【leetcode】8. String to Integer (atoi)

    题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

  9. 【leetcode】443. String Compression

    problem 443. String Compression Input ["a","a","b","b"," ...

随机推荐

  1. centos 7.0 phpize 扩展php

    phpize扩展php模块 phpize 所在目录 /usr/etc/php/bin/phpize 查看当前php配置情况 /usr/etc/php/bin/下面的php [root@localhos ...

  2. 浅谈JavaScript中的Function引用类型

    引言 在JavaScript中最有意思的就是函数了,这一切的根源在于函数实际上是一个对象.每一个函数都是Function类型的实例,而且都和其他引用类型的实例一样具有属性和方法.函数作为一个对象,因此 ...

  3. CentOS 与 RedHat 关系和区别

    转自http://www.aixchina.net/club/archiver/tid-26784.html CentOS 发行版介绍 CentOS 是 Community ENTerprise Op ...

  4. 微信电脑版微信1.1 for Windows更新 可@人/转发撤回消息/可播小视频

    微信电脑版微信1.1 for Windows发布更新了,版本号为1.1.0.18,群聊可@人/可转发撤回消息/可播小视频,功能越来越接近微信手机版了. 本次更新的一些新特点: 群聊中可以@人. 消息可 ...

  5. c#后台替换html标签的方法

     public static string ReplaceHtmlTag(string html)         {              string strText = System.Tex ...

  6. 01什么是ExtJs?

    前言: 我之前是搞EasyUI+KO.js的,由于最近项目需要用到ExtJs.因此.边学边记录我的学习历程,希望能给自己和大家一点帮助. 1.0什么是ExtJs? 首先,什么是ExtJs呢?ExtJs ...

  7. webpack 教程 那些事儿06-gulp+webpack多页

    本篇主要讲述用gulp+webpack构建多页应用 折腾到现在,项目还必须要进行,.vue文件必须要加载,也就是webpack必须引入.时间不多了,抛弃上个方案之后,只能牺牲热加载性能,用gulp+w ...

  8. 【转】CentOS环境下yum安装LAMP(Linux+Apache+Mysql+php)

    此种方法很简单.每次都用源码编译,浪费好多时间啊! 同样的网站程序在Linux下运行要比在windows下快出不少,所以决定使用Linux的发行版CentOS ,本文主要讲解在CentOS下使用yum ...

  9. spring ioc原理

    最近,买了本Spring入门书:spring In Action .大致浏览了下感觉还不错.就是入门了点.Manning的书还是不错的,我虽然不像哪些只看Manning书的人那样专注于Manning, ...

  10. iOS开发——网络篇——数据安全(MD5),HTTPS,检测网络状态

    一.数据安全 1.提交用户的隐私数据一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中服务器的访问日志是黑客攻击的重点对象之一 ...