题意:

判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义:

字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开,直到无法再切,此时生成为一棵二叉树。对二叉树的任一子树可任意交换其左右分支,如果S1可以通过交换变成S2,则S1,S2互为Scramble字符串。

思路:

对于分割后的子串,应有IsScramble(s1[0,i] , s2[0,i]) && IsSCramble(s1[i,length] , s2[i,length])

因为分割后可以交换子串,于是也可能有IsScramble(s1[0,i] , s2[length-i,length]) 同时 IsScramble(s1[i,length] , s2[0,length-i])

注意剪枝条件:如果两串长度不同,false;如果两串所含字符种类不同,false。

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Scramble String.

class Solution
{
public:
bool isScramble(string s1, string s2)
{
if (s1.length() != s2.length())
return false;
if (s1 == s2)
return true;
int check[] = {};
int i;
for (i = ; i < s1.length(); ++i)
{
++check[s1[i] - 'a'];
--check[s2[i] - 'a'];
} for (i = ; i < ; ++i)
{
if (check[i] != )
return false;
} for (i = ; i < s1.size(); i++)
{
if (
(isScramble(s1.substr(, i), s2.substr(, i)) && isScramble(s1.substr(i), s2.substr(i))) || (isScramble(s1.substr(, i), s2.substr(s1.size() - i)) && isScramble(s1.substr(i), s2.substr(, s1.size() - i))))
return true;
}
return false;
}
};

[leetcode] 87. Scramble String (Hard)的更多相关文章

  1. [leetcode]87. Scramble String字符串树形颠倒匹配

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

  2. [LeetCode] 87. Scramble String 搅乱字符串

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

  3. [LeetCode] 87. Scramble String 爬行字符串

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

  4. Leetcode#87 Scramble String

    原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...

  5. 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 ...

  6. leetCode 87.Scramble String (拼凑字符串) 解题思路和方法

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

  7. 【一天一道LeetCode】#87. Scramble String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. 【leetcode】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  9. 【LeetCode】87. Scramble String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 动态规划 日期 题目地址:https://le ...

随机推荐

  1. Another maybe monad library for ruby

    欢迎任何形式的转载,但请务必注明出处:http://www.cnblogs.com/liangjingyang 项目地址:https://github.com/liangjingyang/maybe_ ...

  2. Windows线程生灭(图文并茂)

    一.线程创建 Windows线程在创建时会首先创建一个线程内核对象,它是一个较小的数据结构,操作系统通过它来管理线程.新线程可以访问进程内核对象的所有句柄.进程中的所有内存及同一进程中其它线程的栈. ...

  3. Ubuntu14.04 静态编译安装Qt4.8.6

    ./configure -static -nomake demos -nomake examples -nomake tools -no-exceptions -prefix /usr/local/Q ...

  4. SqlServer 动态SQL(存储过程)中Like 传入参数无正确返回值的问题

    最近在做项目时,以动态Sql进行Like语句查询时发现应该返回的结果却一直返回空,后来发现是写法错误: 错误SQL: DECLARE @0 varchar(20) SET @0 = 'XA-LZ' S ...

  5. Oracle expdp/impdp 使用示例

    1. 创建目录 使用数据泵之前,需要创建一个存放文件的目录. 这个目录要写入Oracle的数据字典中才能识别. (1)先查看一下已经存在的目录: SQL> col owner format a5 ...

  6. Unity Shader 菲涅尔环境反射

    菲涅尔反射描述了一种光学现象,当光照到物体表面时,一部分发生反射,另一部分则进入物体内部,发生折射或散射:相比直接的反射和折射计算,菲涅尔反射更接近真实情况. 可用下面的等式近似计算这种反射效果: F ...

  7. V语言横空出世,C/C++/Java/Python/Go地位不保

    V语言已在github正式开源,目前已收获近9000星,引发开发者的强烈关注. V语言到底是怎样一门语言?已经有了C/C++/Java/Python/Go..., 我们还需要另外一门语言吗? 先看看V ...

  8. Numpy之数组创建

    ndarray 数组除了可以使用 ndarray 构造器来创建外,也可以通过如下方式创建. 一.创建数组 numpy.empty 语法: numpy.empty(shape, dtype = floa ...

  9. HDU 2795:Billboard(线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=2795 Billboard Problem Description   At the entrance to th ...

  10. django基础知识之分页:

    分页 Django提供了一些类实现管理数据分页,这些类位于django/core/paginator.py中 Paginator对象 Paginator(列表,int):返回分页对象,参数为列表数据, ...