题目来源:

  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. 新浪微博布局学习——妙用TabHost

    前言 为了更好的开发Android应用程序,除了熟练掌握基本的UI组件和API外,还需要掌握一些技巧,而这些技巧可以通过阅读一些代码来提高,本系列将与大家分享一些新浪微博布局方面的收获,欢迎交流! 声 ...

  2. hdu1114Piggy-Bank(DP完全背包)

    题意:在ACM可以做任何事情,必须准备和预算获得必要的财政支持.这次行动的主要收入来自不可逆绑定金钱(IBM).背后的想法很简单.每当一些ACM成员有任何小的钱,他把所有的硬币和成小猪银行抛出.你知道 ...

  3. 匹配图片src正则

    preg_match_all('#<img.*?src="([^"]*)"[^>]*>#i', $xstr, $match); /** * 获取图片sr ...

  4. vim常用命令总结 (转)

      在命令状态下对当前行用== (连按=两次), 或对多行用n==(n是自然数)表示自动缩进从当前行起的下面n行.你可以试试把代码缩进任意打乱再用n==排版,相当于一般IDE里的code format ...

  5. textContent和innerHtml

    textContent,innerText, 查询或者设置元素的文本内容. textContent如,html: <p>test gogo</p> javascript中: v ...

  6. JSP标签库

    step1: 定义一个标签处理类:改类必须实现SimpleTag接口,在实际中是拓展它的子类SimpleTagSupport.复写doTag方法 public class MyTag extends ...

  7. Win8安装ASP.net 4.5(转)

    (原文:http://blog.csdn.net/dingxu_ren/article/details/17607451) 今天在我的电脑上部署Web程序时发现页面打不开,网上搜了下是因为先安装的.n ...

  8. Hive入门之UDFS函数

    一.UDFS函数介绍 1. 基本UDF (1)SHOWFUNCTIONS:这个用来熟悉未知函数. DESCRIBE FUNCTION<function_name>; (2)A IS NUL ...

  9. github--新手使用错误分析

    先上去github 或者任意托管的网站.注册账号,新建仓库, 在本地运行Xcode 新建工程,新建工程的时候 勾上本地 的仓库,然后 在本地的项目根目录执行下边的命令: git remote add ...

  10. php错误 mysql_query():supplied argument is not a valid MySQL result resource

    出现这种错误,原因是出现该错误的函数的参数出现了问题 参数出现问题有多种情况: 1.sql查询语句有问题,可能多了一个逗号,少了一个逗号,多了括号之类的: 2.与数据库连接的参数有问题,用户名.密码. ...