【LeetCode】87. 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.
提示:
这道题可以用递归方法较为简单地解决,递归的形式可以这样:
// 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的更多相关文章
- 【LeetCode】87. Scramble String 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 动态规划 日期 题目地址:https://le ...
- 【一天一道LeetCode】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- 【LeetCode】344. Reverse String 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 新构建字符串 原地翻转 日期 题目地址:https://lee ...
- 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】942. DI String Match 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- javascript设计模式详解之策略模式
接上篇命令模式来继续看下js设计模式中另一种常用的模式,策略模式.策略模式也是js开发中常用的一种实例,不要被这么略显深邃的名字给迷惑了.接下来我们慢慢看一下. 一.基本概念与使用场景: 基本概念:定 ...
- Jdk1.6 JUC源码解析(12)-ArrayBlockingQueue
功能简介: ArrayBlockingQueue是一种基于数组实现的有界的阻塞队列.队列中的元素遵循先入先出(FIFO)的规则.新元素插入到队列的尾部,从队列头部取出元素. 和普通队列有所不同,该队列 ...
- PHP导出生成CSV文件
composer 用起来是非常方便的 所以我是依赖composer来做的包管理 1.先安装composer 自行百度一下composer安装以及使用 2.用composer下载安装office包即可 ...
- java面向对象--继承与多态
可以为一个变异单元中的每个类创建一个main方法,只有命令行所调用的那个类的main方法才会被调用,这样方便进行单元测试.继承时,一般将所有的数据成员都指定为private,将所有的方法指定为publ ...
- IT培训行业揭秘(六)
2017年全国的IT职业培训机构的招生数量相比于去年同期都出现了大规模的下滑,虽然目前大学生毕业之后参加培训班的人数依然没有变化,但是目前中小培训机构像雨后春笋般的纷纷建立,他们纷纷抢占市场,为了招生 ...
- DRBD+Heartbeat+Mysql高可用读写分离架构
声明:本案例仅为评估测试版本 注意:所有服务器之间必须做好时间同步 架构拓扑 IP信息: Heartbeat安装部署 1.安装heartbeat(主备节点同时安装) [root@master1 ~]# ...
- [HDU1001] Sum Problem
Problem Description Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge). In this problem, ...
- java面试题之int和Integer的区别
int和Integer的区别 1.Integer是int的包装类,int则是java的一种基本数据类型 2.Integer变量必须实例化后才能使用,而int变量不需要 3.Integer实际是对象的引 ...
- SQL万能语句-经典操作
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...
- cpp(第七章)
1.c++提供了3种表示c—风格字符串方法:字符数组,字符串常量,字符串指针.其中字符数组并不一定是字符串,以空值字符'\0'来结束的字符数组时字符串. 2.函数参数为数组时,虽然减少了时间和内存的使 ...