【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 ...
随机推荐
- $>_<$
Hello word! 从jdk环境变量的配置,myeclipse的安装,tomcat的部署和使用,面向对象的编程思想,什么是java. 思维从模糊到清晰,一路摸索,不见泰山!
- Natas Wargame Level 16 Writeup(Content-based Blind SQL Injection)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAqwAAADhCAYAAAANm+erAAAABHNCSVQICAgIfAhkiAAAIABJREFUeF
- EMC在线测试题目及答案 绿色为正确答案,红色为错误答案
1. 以下哪一项技术可以将IT的物理资源放在一个共享池中以及提升它们的利用率? 分区 虚拟化 协调 LUN 屏蔽 2. 哪一项是EMC的基于块-存储(block-based)的高端存储? Atmos ...
- Tomcat Server处理一个http请求过程
假设来自客户端的请求为: http://localhost:8080/lizhx/lizhx_index.jsp 请求被发送到本机端口8080,被在那里侦听的Coyote HTTP/1.1 Conne ...
- 11、借助POI实现Java生成并打印excel报表(2)
11.POI打印功能 11.1.常用模块形式: HSSFPrintSetup printSetup = sheet.getPrintSetup(); printSetup.setVResolution ...
- 【JAVAWEB学习笔记】28_jquery加强:json数据结构、jquery的ajax操作和表单校验插件
Ajax-jqueryAjax 今天内容: 1.json数据结构(重点) 2.jquery的ajax操作(重点) 3.jquery的插件使用 一.json数据结构 1.什么是json JSON(Jav ...
- Java编程之反射中的注解详解
"注解"这个词,可谓是在Java编程中出镜率比较高,而且也是一个老生常谈的话题.我们之前在聊Spring相关的东西时,注解是无处不在,之前我们简单的聊过一些"注解&quo ...
- JavaScript中的该如何[更好的]做动效
在用js写动画的时候,无非使用 setTimeout/setInterval 或者 requestAnimationFrame 来处理动画(在jquery的代码里也是这么干的),本文主要为了记录下两者 ...
- sqlserver使用job删除过期备份文件
享下链接:http://blog.csdn.net/xieyufei/article/details/33770067(注意这里主要说明怎么设置删除过期备份文件) 先说下sqlserver使用job删 ...
- 【 js 基础 】【 源码学习 】源码设计 (持续更新)
学习源码,除了学习对一些方法的更加聪明的代码实现,同时也要学习源码的设计,把握整体的架构.(推荐对源码有一定熟悉了之后,再看这篇文章) 目录结构:第一部分:zepto 设计分析第二部分:undersc ...