作者: 负雪明烛
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翻转二叉树子节点的题目。这个题不过是把树变成了字符串而已。

这个题的重点之一就是要合理的划分字符串从而形成两棵不同的左右子树,进而对左右子树递归。因为事先不知道在哪里进行分割,所以直接对每个可以划分的位置进行遍历分割。判断是否两个子串能否通过翻转变成相等的时候,需要保证传给函数的子串长度是相同的。因此:

  1. s1的[0:i]和s2[0:i]作为左子树,s1[i:N]和s2[i:N]作为右子树
  2. 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++)的更多相关文章

  1. Leetcode:Scramble String 解题报告

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  2. [leetcode]87. Scramble String字符串树形颠倒匹配

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  3. [leetcode] 87. Scramble String (Hard)

    题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...

  4. leetCode 87.Scramble String (拼凑字符串) 解题思路和方法

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  5. [LeetCode] 87. Scramble String 搅乱字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  6. [LeetCode] 87. Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  7. Leetcode#87 Scramble String

    原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...

  8. 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 ...

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

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

随机推荐

  1. R语言与医学统计图形-【27】ggplot2图形组合、字体、保存

    ggplot2绘图系统--图形组合.字体选择.保存输出 1.图形组合 一页多图在基础包中利用par和layout函数来切分画布. ggplot2是先铺好网格背景,再进行绘图,所以要通过切分网格背景来实 ...

  2. perl 数组快速去除重复元素

    这里记录两种perl数组去重的办法,一种利用哈希(hash),一种直接利用perl自带的模块List::MoreUtils内部的函数uniq. 一.利用hash去重 示例代码如下: 1 #!/usr/ ...

  3. Vue.js知识点总结

    1. Vue.js简介 1.1 Vue.js简介 1.2 创建一个vue实例 2. Vue.js基础 2.1 模板语法 2.2 环境搭建 2.3 生命周期钩子

  4. 非标准的xml解析器的C++实现:一、思考基本数据结构的设计

    前言: 我在C++项目中使用xml作为本地简易数据管理,到目前为止有5年时间了,从最初的全文搜索标签首尾,直到目前项目中实际运用的类库细致到已经基本符合w3c标准,我一共写过3次解析器,我自己并没有多 ...

  5. sed 修改文件

    总结 正确的修改进文件命令(替换文件内容):sed -i "s#machangwei#mcw#g" mcw.txt 正确的修改追加进文件命令(追加文件内容):sed -i &quo ...

  6. C语言中的指针与整数相加的值计算

    以下分三种情况: 1. 指针 + 整数值 2. 整数 + 整数  3. 指针强制转换为另一个类型后(指针或者是整数)  +  整数 测试例子: 1 struct AAA{ int a; char b[ ...

  7. Output of C++ Program | Set 11

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  8. Type of 'this' pointer in C++

    In C++, this pointer is passed as a hidden argument to all non-static member function calls. The typ ...

  9. 【Linux】【Problems】在fedora 9上解决依赖问题

    summary: 在32bit的fedora9上安装EMC客户端遇到无法解决的依赖问题 detail: rpm 安装: [root@hcszmons02 tmp]# rpm -ivh lgtoclnt ...

  10. Springboot(1) helloworld 搭建环境

    一 .springboot 运行环境: 1. jdk1.8:Spring Boot 推荐jdk1.7及以上:java version "1.8.0_112" 2.–maven3.x ...