[LeetCode]题解(python):087-Scramble String
题目来源:
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的更多相关文章
- 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 ...
- LeetCode 笔记系列 19 Scramble String [合理使用递归]
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- LeetCode之“动态规划”:Scramble String
题目链接 题目要求: Given a string s1, we may represent it as a binary tree by partitioning it to two non-emp ...
- 087 Scramble String 扰乱字符串
给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树.下图是字符串s1 = "great"的一种可能的表示形式. great / \ ...
- 【一天一道LeetCode】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Leetcode:Scramble String 解题报告
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【leetcode】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- 【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 ...
随机推荐
- 整理Linux查看系统日志的一些经常使用命令
整理Linux查看系统日志的一些经常使用命令 last -a 把从何处登入系统的主机名称或ip地址,显示在最后一行. -d 指定记录文件.指定记录文件.将IP地址转换成主机名称. -f <记录文 ...
- 第十七周oj刷题——Problem B: 分数类的四则运算【C++】
Description 编写分数类Fraction,实现两个分数的加.减.乘和除四则运算.主函数已给定. Input 每行四个数,分别表示两个分数的分子和分母,以0 0 0 0 表示结束. Outpu ...
- 【九】注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)
上一篇我们简单的介绍了一下RoboGuice的使用([八]注入框架RoboGuice使用:(Your First Injected Fragment)),今天我们来看下服务(Service)和广播接受 ...
- 玩转kindle paperwhite: 如何越狱,安装强大外挂软件koreader
NOTICE 1: 在更新kpvbooklet和使用最新版本的koreader(v2013.03-211)时候,会出现pdf文档无法重排的错误.亲测. 如果你是使用的最新版本koreader且出现上述 ...
- HiveQL与SQL区别
转自:http://www.aboutyun.com/thread-7327-1-1.html 1.Hive不支持等值连接 SQL中对两表内联可以写成:select * from dual a,dua ...
- hdu 4612 Warm up 有重边缩点+树的直径
题目链接 Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tot ...
- java 线程学习
转载:详见处http://lavasoft.blog.51cto.com/62575/27069 Java多线程编程总结 下面是Java线程系列博文的一个编目: Java线程:概念与原理 ...
- 射频识别技术漫谈(28)——基于MF1射频卡的酒店门锁设计
电子门锁是现代星级酒店管理电子化.智能化的重要电子设备.相较于传统的机械锁,基于RFID技术的电子门锁使用方便,易于管理,安全性高,可实现对开锁用户的分优先级自动管理,对房间入住信息实现自动统计与报表 ...
- 一步一步学习SignalR进行实时通信_3_通过CORS解决跨域
原文:一步一步学习SignalR进行实时通信_3_通过CORS解决跨域 一步一步学习SignalR进行实时通信\_3_通过CORS解决跨域 SignalR 一步一步学习SignalR进行实时通信_3_ ...
- tomcat链接mysql时超时报错java.io.EOFException: Can not read response from server. Expected to read 4 bytes,
需要在配置文件里加上下面就ok了 <property name=”minEvictableIdleTimeMillis” value=”1800000″ /> <property n ...