【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内置对象:数组对象
1.数组对象的创建 1.设置一个长度为0的数组 var myarr=new array(); 2.设置一个长度为n的数组 var myarr=new arr(n); 3.声明一个赋值的指定长度 ...
- PS不能存储,因为程序错误
当PS中遇到不能存储文件,因为程序错误时,可以这样: http://www.zcool.com.cn/article/ZMTgwOTQw.html
- java泛型探索——泛型类
本文主要讨论一下如何声明泛型类,讨论的范围涉及构造函数.静态成员.内部类. 构造函数 泛型的类型参数首先声明在首部: public class Pair<T,U> { private fi ...
- JVM方法调用
当我们站在JVM实现的角度去看方法调用的时候,我们自然会想到一种分类: 1.编译代码的时候就知道是哪个方法,永远不会产生歧义,例如静态方法,private方法,构造方法,super方法. 2.运行时才 ...
- 用JS添加和删除class类名
下面介绍一下如何给一个节点添加和删除class名 添加:节点.classList.add("类名"): 删除:节点.classList.remove("类名") ...
- EntityFramework6.X 之DbContex
DbContext 数据库上下文DbContext是ObjectContext代表之一负责管理在运行时从数据库取数据.跟踪数据库的变更.对实体类映射到数据中持久化操作等,表示工作单元和存储库模式的组合 ...
- DOM4J介绍与代码示例(2)-XPath 详解
XPath 详解,总结 XPath简介 XPath是W3C的一个标准.它最主要的目的是为了在XML1.0或XML1.1文档节点树中定位节点所设计.目前有XPath1.0和 XPath2.0两个版本.其 ...
- fiddler导致页面确定按钮无法使用(测试遇到的问题经验)
这几天在测试的是遇到几个问题,就是在删除或者保存有些提示信息的时候 比如下面这种: 点击确定的时候,一直无响应,换了几台电脑其他电脑都是正常的,本机清楚缓存.关闭浏览器重新打开.重启电脑都试过了了就是 ...
- jmeter 环境部署、数据库设置、分布式设置、多网卡配置等随笔
<!-- linux系统修改系统环境变量 系统语言-->[root@web-249 ~]# env|grep LANGLANG=zh_CN.UTF-8[root@web-249 ~]# ...
- Java对字符串进行的操作
本篇总结归纳对字符串或数组进行相关操作问题 数组倒序输出 查找字符串中第一次重复的字符 查找字符串中第一次没有重复的字符 删除字符串中重复的元素 倒序输出问题 第一种:对于数组 public int[ ...