题意:

判断两个字符串是否互为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. UILabel实现自适应宽高需要注意的地方

    需求如下:   需要显示2行文字,宽度为 SCREEN_Width - 40 高度为两行文本的自适应高度 需要在此UILabel 下面添加imageView , 因此UIlabel 的高度需要准确,不 ...

  2. Linux编辑器Vim和Emacs入门

    sudo 命令 debian系统没有自带,需要安装: apt-get install sudo 安装位置为 /usr/bin/sudo,对应配置文件为 /etc/sudoers sudoers授权格式 ...

  3. ZooKeeper学习第八期——ZooKeeper伸缩性(转)

    转载来源:https://www.cnblogs.com/sunddenly/p/4143306.html 一.ZooKeeper中Observer 1.1 ZooKeeper角色 经过前面的介绍,我 ...

  4. java.nio.Buffer源码阅读

    Java 自从 JDK1.4 起,对各种 I/O 操作使用了 Buffer 和 Channel 技术.这种更接近于操作系统的的底层操作使得 I/O 操作速度得到大幅度提升,下面引用一段<Java ...

  5. 管理分布式session的四种方式。

    应用服务器的高可用架构设计最为理想的是服务无状态,但实际上业务总会有状态的,以session记录用户信息的例子来讲,未登入时,服务器没有记入用户信息的session访问网站都是以游客方式访问的,账号密 ...

  6. Hadoop 学习之路(七)—— HDFS Java API

    一. 简介 想要使用HDFS API,需要导入依赖hadoop-client.如果是CDH版本的Hadoop,还需要额外指明其仓库地址: <?xml version="1.0" ...

  7. Mac下安装redis5.0 与命令

    参考链接:https://blog.csdn.net/zyp1376308302/article/details/84257606 参开链接2:https://www.cnblogs.com/guan ...

  8. php中使用trait设计单例

    trait Singleton { private static $instace = null; private function __construct() { } private functio ...

  9. 你需要知道的c# Timer 的垃圾回收机制。

    通常我们需要定时执行一段任务的时候,我们就需要定时器,这时我们就可以使用c# System.Threading空间中的 Timer定时器;他是个异步定时器,时间到时每次都是在线程池中分配一个线程去执行 ...

  10. Codeforces Round #563 (Div. 2)A

    A. Ehab Fails to Be Thanos 题目链接:http://codeforces.com/contest/1174/problem/A 题目 You’re given an arra ...