[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 ...
随机推荐
- 【原创】重绘winform的GroupBox
功能:重绘winform的GroupBox,以便调整边框颜色和边框宽度 using System; using System.Collections.Generic; using System.Com ...
- 制作U盘启动盘(以CentOS6.3为例)
借助UltraISO(软碟通),自己百度下载一个即可(同样适用于制作Windows启动盘). 选择文件→打开,选择ISO镜像所在目录,如下两幅图所示:
- C#控件大小随窗体大小等比例变化
相信很多博友在开发初次接触学习C# winForm时,当窗体大小变化时,窗体内的控件并没有随着窗体的变化而变化,最近因为一个项目工程的原因,也需要解决这个问题.通过查阅和学习,这个问题得到了解决,或许 ...
- sqlserver存储过程及易错点
create PROCEDURE [dbo].[xiao_adduser] @username NVARCHAR(), @password NVARCHAR(), @adddate DATETIME ...
- <转>四个重要属性——Action、Data、Category、Extras
Intent作为联系各Activity之间的纽带,其作用并不仅仅只限于简单的数据传递.通过其自带的属性,其实可以方便的完成很多较为复杂的操作.例如直接调用拨号功能.直接自动调用合适的程序打开不同类型的 ...
- php基础教程笔记
php的环境搭建很简单,从网上下载wamp service 2.5,官方网址http://www.wampserver.com/,有32位和64位的,必须下载跟系统一致的版本,不然会出现奇怪的错误,这 ...
- codeforces 535D. Tavas and Malekas KMP
题目链接 又复习了一遍kmp....之前都忘光了 #include<bits/stdc++.h> using namespace std; #define pb(x) push_back( ...
- ora-01653: unable to extend table sys.aud$ by 8192 in tablespac system[转载]
在用sqlplus user/password@truth登录数据库时报如下错误:ORA-00604: error occurred at recursive SQL level 1ORA-01653 ...
- android和Vitamio使用比较
在开始接触udp组播的时候先使用的Vitamio,播放时候声音卡顿 画面也会出现卡顿,后来又使用了VLC,画面挺好,,但是声音卡顿.最后不断测试发现是由于设备底层驱动处理视频部分有问题,导致程序播出的 ...
- BZOJ 1103 [POI2007]大都市meg(树状数组+dfs序)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1103 [题目大意] 给出一棵树,每条边的经过代价为1,现在告诉你有些路不需要代价了, ...