【LeetCode】87. Scramble String 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/scramble-string/description/
题目描述
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.
Example 1:
Input: s1 = "great", s2 = "rgeat"
Output: true
Example 2:
Input: s1 = "abcde", s2 = "caebd"
Output: false
题目大意
题目是混杂的字符串。一个字符串s1可以表达成一棵二叉树的形式,从而把一棵二叉树的所有叶子节点从左到右遍历一遍就能得到源字符串。现在我们要做一些翻转二叉树的操作,即把某些位置的二叉树进行翻转,这样从左到右的叶子节点又会串成另外一个字符串s2。现在要我们判断给定s1能不能通过翻转某些位置的二叉树形式得到另外一个字符串s2.
解题方法
递归
这个题某种意义上来说就是让我们来判断两棵二叉树是否能够通过翻转某些子树而互相得到,也就是951. Flip Equivalent Binary Trees翻转二叉树子节点的题目。这个题不过是把树变成了字符串而已。
这个题的重点之一就是要合理的划分字符串从而形成两棵不同的左右子树,进而对左右子树递归。因为事先不知道在哪里进行分割,所以直接对每个可以划分的位置进行遍历分割。判断是否两个子串能否通过翻转变成相等的时候,需要保证传给函数的子串长度是相同的。因此:
- s1的[0:i]和s2[0:i]作为左子树,s1[i:N]和s2[i:N]作为右子树
- s1的[0:i]和s2[N - i:N]作为左子树,s1的[i:N]和s2[0:N-i]作为右子树
其中左子树的两个字符串的长度都是i,右子树的两个字符串的长度都是N - i.如果上面两种情况有一种能够成立,则源字符串s1能够变成s2。
由于使用了递归,所以终止条件一定要写,很简单的对长度是0、长度是1、两个字符串排序之后是否相等进行判断。
Python代码如下:
class Solution(object):
def isScramble(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: bool
"""
N = len(s1)
if N == 0: return True
if N == 1: return s1 == s2
if sorted(s1) != sorted(s2):
return False
for i in range(1, N):
if self.isScramble(s1[:i], s2[:i]) and self.isScramble(s1[i:], s2[i:]):
return True
elif self.isScramble(s1[:i], s2[-i:]) and self.isScramble(s1[i:], s2[:-i]):
return True
return False
C++代码如下:
class Solution {
public:
bool isScramble(string s1, string s2) {
if (s1.size() != s2.size()) return false;
const int N = s1.size();
if (N == 0) return true;
if (N == 1) return s1 == s2;
string s1copy = s1;
string s2copy = s2;
sort(s1copy.begin(), s1copy.end());
sort(s2copy.begin(), s2copy.end());
if (s1copy != s2copy) return false;
for (int i = 1; i < N; i++) {
if ((isScramble(s1.substr(0, i), s2.substr(0, i)) && isScramble(s1.substr(i), s2.substr(i))))
return true;
if ((isScramble(s1.substr(0, i), s2.substr(N - i)) && isScramble(s1.substr(i), s2.substr(0, N - i))))
return true;
}
return false;
}
};
动态规划
待补。
日期
2018 年 12 月 11 日 —— 双十一已经过去一个月了,真快啊。。
【LeetCode】87. Scramble String 解题报告(Python & C++)的更多相关文章
- Leetcode:Scramble String 解题报告
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- [leetcode]87. Scramble String字符串树形颠倒匹配
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [leetcode] 87. Scramble String (Hard)
题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...
- leetCode 87.Scramble String (拼凑字符串) 解题思路和方法
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [LeetCode] 87. Scramble String 搅乱字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [LeetCode] 87. Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- Leetcode#87 Scramble String
原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...
- 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 ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
随机推荐
- python15正则表达式
------------恢复内容开始------------ python实现实现实现实现 import re #将表达式编译,返回一个对象, pattern = re.compile(r" ...
- Python基础之流程控制for循环
目录 1. 语法 2. for+break 3. for+continue 4. for循环嵌套 1. 语法 while循环可以对任何内容循环,但循环次数不可控 for循环基于容器类型的长度,循环次数 ...
- Oracle-distinct()用法、count(distinct( 字段A || 字段B))是什么意思?distinct多个字段
0.distinct用法 在oracle中distinct的使用主要是在查询中去除重复出现的数据 直接在字段前加distinct关键字即可,如:select distinct 名字 from tabl ...
- ChromeDriver的安装和使用
用于驱动Chrome浏览器,适用于有界面的操作系统. 一.安装ChromeDriver 要先安装Chrome浏览器,然后安装ChromeDriver. 官方网站:https://sites.googl ...
- 【模板】一般图最大匹配(带花树算法)/洛谷P6113
题目链接 https://www.luogu.com.cn/problem/P6113 题目大意 给定一个 \(n\) 个点 \(m\) 条边的无向图,求该图的最大匹配. 题目解析 二分图最大匹配,一 ...
- 日常Java 2021/10/24
Java ArrrayList ArrayList类是一个可以动态修改的数组,没有固定大小的限制,可以在任何时候添加或者删除元素 ArrayList类在java.util包中使用之前需要引用 E:泛型 ...
- nit是虱子的卵
如题.[牛津] (egg of a) louse or other parasitic insect 虱或其他寄生虫(的卵). 忘了在那个帖子里说nit: 虱子了. 为了凑字数,迄今为止六级/考研单词 ...
- A Child's History of England.39
He had become Chancellor, when the King thought of making him Archbishop. He was clever, gay, well e ...
- 零基础学习java------21---------动态代理,java8新特性(lambda, stream,DateApi)
1. 动态代理 在一个方法前后加内容,最简单直观的方法就是直接在代码上加内容(如数据库中的事务),但这样写不够灵活,并且代码可维护性差,所以就需要引入动态代理 1.1 静态代理实现 在讲动态代理之前, ...
- h5移动端设备像素比dpr介绍
首先介绍一下概念 devicePixelRatio其实指的是window.devicePixelRatio window.devicePixelRatio是设备上物理像素和设备独立像素(device- ...