[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 ...
随机推荐
- JBoss+Ant实现EJB无状态会话bean实例
EJB分为session bean.entity bean.message-driven bean,session bean又分为无状态会话bean和有状态会话bean. session bean负责 ...
- Mahout源码MeanShiftCanopyDriver分析之二MeanShiftCanopyMapper仿造
首先更正一点,昨天处理数据的时候是有问题的,直接从网页中拷贝的文件的空格是有问题的,直接拷贝然后新建的文件中的空格可能有一个两个.三个的,所以要把两个或者三个的都换为一个,在InputMapper中下 ...
- 教你爱上Blocks(闭包)
传值 Blocks是C语言的扩充功能:带有自动变量(局部变量)的匿名函数.通过Blocks,源代码中就能使用匿名函数,即不带名称的函数.在我们 的工作中,命名占据了很大一部分,函数名,变量名,属性名, ...
- DataTable转换实体类
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data. ...
- centos下zookeeper集群搭建
单机模式: 1) 首先下载zookeeper压缩包, 这里采用zookeeper3.4.8.... wget http://mirror.bit.edu.cn/apache/zookeeper/zo ...
- 【LeetCode题意分析&解答】43. Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 一个简单的web框架实现
一个简单的web框架实现 #!/usr/bin/env python # -- coding: utf-8 -- __author__ = 'EchoRep' from wsgiref.simple_ ...
- ElaticSearch网站
http://www.tuicool.com/articles/r2QJVr http://so.searchtech.pro/articles/2013/06/16/1371392427213.ht ...
- Advanced Replication同步复制实验(基于Trigger&基于Materialized View)
1. 高级复制和流复制介绍 1.1 高级复制(Advanced Replication) 高级复制也称为对称复制,分为多主体站点复制(Multiple Master Rplication).物化视图站 ...
- c++实现将表达式转换为逆波兰表达式
https://github.com/Lanying0/lintcode 所属: 数据结构->线性结构->栈 问题: 给定一个表达式字符串数组,返回该表达式的逆波兰表达式(即去掉括号). ...