题目:

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.

提示:

这道题可以用递归方法较为简单地解决,递归的形式可以这样:

// recursive solution, the idea is based on the binary tree struct.
for (int i = ; i < s1.length(); ++i) {
if (isScramble(s1.substr(, i), s2.substr(, i)) && isScramble(s1.substr(i), s2.substr(i))) {
return true;
}
if (isScramble(s1.substr(, i), s2.substr(s2.length() - i)) && isScramble(s1.substr(i), s2.substr(, s2.length() - i))) {
return true;
}
}

结合题目的意思,如果两个单词是scramble的话,那么他们可能会有非叶子节点进行了逆序的操作,因此我们需要同时判断:

  • s1的左子节点与s2的左子节点,s1的右子节点与s2的右子节点
  • s1的左子节点与s2的右子节点,s1的右子节点与s2的左子节点

另外在进行递归前,需要对字符串是否相等,长度是否相等,是否是变位词进行判断。

代码:

class Solution {
public:
bool isScramble(string s1, string s2) {
// equal ?
if (s1 == s2) {
return true;
}
// if length are differrent, they can not be scramble
if (s1.length() != s2.length()) {
return false;
}
// just like anagram
vector<int> a(, );
for (int i = ; i < s1.length(); ++i) {
++a[s1[i]];
--a[s2[i]];
}
for (int i = ; i < s1.length(); ++i) {
if (a[s1[i]] != ) {
return false;
}
}
// recursive solution, the idea is based on the binary tree struct.
for (int i = ; i < s1.length(); ++i) {
if (isScramble(s1.substr(, i), s2.substr(, i)) && isScramble(s1.substr(i), s2.substr(i))) {
return true;
}
if (isScramble(s1.substr(, i), s2.substr(s2.length() - i)) && isScramble(s1.substr(i), s2.substr(, s2.length() - i))) {
return true;
}
}
return false;
}
};

【LeetCode】87. Scramble String的更多相关文章

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

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

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

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

  3. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  4. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  5. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

  6. 【LeetCode】344. Reverse String 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 新构建字符串 原地翻转 日期 题目地址:https://lee ...

  7. 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)

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

  8. 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)

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

  9. 【LeetCode】942. DI String Match 解题报告(Python)

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

随机推荐

  1. [刷题]算法竞赛入门经典(第2版) 4-4/UVa253 - Cube painting

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) #include<iostream> char str[15]; v ...

  2. 转:Linux中文显示乱码?如何设置centos显示中文

    怎么设置Linux系统中文语言,这是很多小伙伴在开始使用Linux的时候,都会遇到一个问题,就是终端输入命令回显的时候中文显示乱码.出现这个情况一般是由于没有安装中文语言包,或者设置的默认语言有问题导 ...

  3. 第二章 基本图像处理(Image Processing)

    主要内容: 图像的表示----介绍图像是如何表示的,以及所有基本操作的作用对象 高斯滤波-----滤波操作的原理与应用 图像金字塔-----高斯和拉普拉斯 边缘检测-----Sobel算子和Lapla ...

  4. 自坑实录 - Asp.net MVC中无法使用@Ajax.BeginForm问题解决

    创建空的web项目,通过Nuget引用mvc组件来搭建空的MVC项目时, 在视图页面中无法使用@Ajax.BegForm来进行异步提交数据, 而新建默认的MVC模板项目却能够正常使用@Ajax.Beg ...

  5. 深入理解Struts2----类型转换

         之前的一系列文章主要介绍了有关Struts2的一些基本用法和部分的简单原理,但是始终没有介绍有关拦截器的相关内容,从本篇开始我们将从另一个角度去深入理解框架的使用,核心还是拦截器,但本篇首先 ...

  6. php jquery+ajax写批量删除

      为了美观,我还是引入了bootstrap的模态框,我引入的是自己的数据库 library中的一张表 名为:maninfo表 是一张个人信息表  表的加载我就不写了,比较简单,  大概写一下需要的按 ...

  7. progressBar的使用

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  8. 为什么重写equals时必须重写hashCode方法?(转发+整理)

    为什么重写equals时必须重写hashCode方法? 原文地址:http://www.cnblogs.com/shenliang123/archive/2012/04/16/2452206.html ...

  9. Linux常用网络测试命令

    Linux常用网络测试命令 1. ifconfig     可以使用ifconfig命令来配置并查看网络接口的配置情况.    例如: (1) 配置eth0的IP地址, 同时激活该设备. #ifcon ...

  10. VR全景智慧城市搭建掀起实体市场潮流

    在互联网时代的今天,用户体验至上,全景智慧城市搭建作为一个新型的科技展示技术,通过新颖的广告方式更能吸引用户眼球,足不出户,观看现场实景,达到沉浸式体验.在这样的大环境下,全景智慧城市搭建开启了VR全 ...