题目来源:

  https://leetcode.com/problems/scramble-string/


题意分析:

  给定一个字符串,字符串展成一个二叉树,如果二叉树某个或多个左右子树颠倒得到的新字符串称为scramble。给两个字符串,判断是否互为scramble。


题目思路:

  这是一个动态规划问题,把其中字符拆成两部分,如果这两部分满足是scramble,那么整个字符串就满足scramble。


代码(python):

  

class Solution(object):
def isScramble(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: bool
"""
if len(s1) != len(s2):
return False
if s1 == s2:
return True
tmp1,tmp2 = list(s1),list(s2)
tmp1.sort();tmp2.sort()
if tmp1 != tmp2:
return False
size = len(s1)
for i in range(1,size):
if self.isScramble(s1[i:],s2[i:]) and self.isScramble(s1[:i],s2[:i]):
return True
if self.isScramble(s1[i:],s2[:size - i]) and self.isScramble(s1[:i],s2[size - i:]):
return True
return False

[LeetCode]题解(python):087-Scramble String的更多相关文章

  1. Java for LeetCode 087 Scramble String

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

  2. LeetCode 笔记系列 19 Scramble String [合理使用递归]

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

  3. LeetCode之“动态规划”:Scramble String

    题目链接 题目要求: Given a string s1, we may represent it as a binary tree by partitioning it to two non-emp ...

  4. 087 Scramble String 扰乱字符串

    给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树.下图是字符串s1 = "great"的一种可能的表示形式.    great   /    \ ...

  5. 【一天一道LeetCode】#87. Scramble String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. Leetcode:Scramble String 解题报告

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

  7. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  8. 【leetcode】Scramble String

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

  9. 【LeetCode练习题】Scramble String

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

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

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

随机推荐

  1. The Water Problem(排序)

    The Water Problem Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  2. STL 源代码剖析 算法 stl_algo.h -- merge sort

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie merge sort ----------------------------------- ...

  3. ListView分割线

    在开发中遇到需要ListView 中每一个条目后面都有个分隔线,但是总是发现最后一个条目后面没有分隔符,后来查到原因,是因为ListView的layout_height=“wrap_content” ...

  4. 解决Android中TextView首行缩进的问题

    方式一:(推荐) setText("\u3000\u3000"+xxxxx); 方式二:这种方式不同分辨率会有问题 setText(""+xxxxx); 半角: ...

  5. spm3 基本

    spm3 命令 spm init //初始化一个spm模块,会生成基本配置以及测试文件等(下图). //注 初始化以后一般需要 鲜执行一下 spm install 安装默认依赖模块 index.js就 ...

  6. JavaSE思维导图(一)

  7. Centos7 安装mysql数据库

    第一步:下载数据库文件 # wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm 看到下载成功 第二步: # r ...

  8. eclipse注解快捷键

    Search 功能:全局文件内容搜索快捷键: Ctrl + H -------------------------------------------------------------------- ...

  9. java中的四则运算

    代码的思路是通过正则判断计算每个最小的计算单元.以下是代码: package cn.com.lawchat.forpublicmvc.util; import java.math.BigDecimal ...

  10. sqlplus命令手册

    show errorshow allshow usersqlplus show和set命令是两条用于维护SQLPlus系统变量的命令 : SQL> show all --查看所有系统变量值 SQ ...