87. Scramble String Leetcode Python

果蔬及菜品识别系统 阅读详情

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

Below is one possible representation of s1 = "great":

    great
   /    \
  gr    eat
 / \    /  \
g   r  e   at
           / \
          a   t

To scramble the string, we may choose any non-leaf node and swap its two children.

For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".

    rgeat
   /    \
  rg    eat
 / \    /  \
r   g  e   at
           / \
          a   t

We say that "rgeat" is a scrambled string of "great".

Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".

    rgtae
   /    \
  rg    tae
 / \    /  \
r   g  ta  e
       / \
      t   a

We say that "rgtae" is a scrambled string of "great".

Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.



This problem can be solved with brute Force.

class Solution:
    # @param s1, a string
    # @param s2, a string
    # @return a boolean
    def isScramble(self, s1, s2):
        n, m = len(s1),len(s2)
        if n!=m or sorted(s1)!=sorted(s2):
            return False
        if n < 4 or s1 == s2:
            return True
        for i in range(1, n):
            if (self.isScramble(s1[:i],s2[:i]) and self.isScramble(s1[i:], s2[i:]) )or (self.isScramble(s1[:i], s2[-i:]) and self.isScramble(s1[i:], s2[:-i]) ):
                return True
        return False
        
            



VScode高效开发STM32:头文件路径配置全解析 本文详细解析了在VScode中高效开发STM32项目的核心步骤——头文件路径配置。针对常见的“找不到头文件”问题,文章深入剖析了IntelliSense的工作原理,并提供了手把手创建和解读c_cpp_properties.json配置文件的完整指南,包括includePath、defines、compilerPath等关键项的设置,帮助开发者彻底解决红色波浪线错误,实现精准的代码提示与跳转。 阅读详情

相关推荐

Arduino安装图文教程:小白也能轻松上手

手把手带你完成Arduino安装全过程,无论你是编程新手还是硬件小白,都能快速搭建开发环境。结合arduino安装教程与实操步骤,避开常见错误,让创作即刻启程。

weixin_35509395的博客 1121

LeetCode 87 Scramble String (Python详解及实现)

【题目】 Given a string s1, we may represent it as abinary tree by partitioning it to two non-empty substrings recursively.   Below is one possible representation of s1= "great":      great   /    \

toplatona 698

BIM模型导入SuperMap

目录 一、超图 BIM插件安装 1.1 插件下载 1.2 安装 二、BIM模型导出 2.1 Revit插件导出 2.2 SuperMap导入工具 三、SuperMap BIM数据处理与服务发布 四、SuperMap三维数据处理中关于模型高程修改方法 一、超图 BIM插件安装 1.1 插件下载 下载链接:http://support.supermap.com.cn/DownloadCenter/DownloadPage.aspx?tt=ProductAAS&id=134 1..

爱晒太阳的风 3629

【LeetCode】87. Scramble String 解题报告(Python & C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法递归动态规划日期 题目地址:https://leetcode.com/problems/scramble-string/description/ 题目描述 Given a string s1, we may represent it as a binary ...

负雪明烛 1592

CHARLS数据库实战指南:从注册到数据下载的完整流程解析

本文详细解析了CHARLS数据库从注册、申请到数据下载的完整实战流程。作为中国中老年人健康与养老研究的权威纵向数据源,CHARLS数据库以其免费、高质量的特点成为学术研究的重要工具。指南重点介绍了如何高效完成账号注册与激活、数据申请与审批,并强调了数据下载后理解复杂抽样设计与使用调查权重的关键性,帮助研究者避开常见陷阱,顺利获取并准备分析数据。

weixin_28338005的博客 544

机器学习-深嵌交叉验证(Nested Cross-Validation)

Section I: Code and Analyses 第一部分:代码 from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC from...

Santorinisu Blogs 2734

Leetcode 87. 扰乱字符串 Java/Python

给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树。 下图是字符串 s1 = "great" 的一种可能的表示形式。 great / \ gr eat / \ / \ g r e at / \ a t 在扰乱这个字符串的过程中,我们可以挑选任何一个非叶节点,然...

u014626513的博客 667

高精度地图数据的结构

本文是个人在学习高精度地图数据结构的心得,主要是描述高精度地图的几层数据结构以及元数据信息。

qq_40664711的博客 5587

利用python 完成 leetcode87 扰乱字符串

给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树。 下图是字符串 s1 = “great” 的一种可能的表示形式。 great / \ gr eat / \ / \ g r e at / \ a t 在扰乱这个字符串的过程中,我们可以挑选任何一个非叶节点,然后交换它...

找到工作之前一天十篇的博客 305

海波龙(Hyperion,现属 Oracle)合并报表以 HFM(Hyperion Financial Management)为核心系统,方法论核心是以控制为基础确定合并范围,通过维度建模统一数据口径

摘要:海波龙合并报表系统(HFM)采用"规则驱动、维度建模"的方法论,通过四个核心环节实现自动化合并:确定合并范围、数据标准化、自动抵销调整及报表生成。系统支持多会计准则,以控制为基础划分合并方法(完全合并/权益法/成本法),通过维度建模统一数据口径,自动处理内部交易抵销、外币折算等复杂操作。业务操作流程涵盖数据采集、转换、抵销调整到报表生成全过程,关键控制点包括内部交易匹配、股权变动处理等。最终输出合规合并报表及完整审计线索,满足监管与管理需求。(150字)

金牌架构师 1321

无需SLAM建图,直接生成Gazebo仿真环境的二维代价地图的功能包pgm_map_creator适配ROS Noetic编译报错及解决方法

在ROS Noetic中,使用`pgm_map_creator`功能包直接生成Gazebo仿真环境的二维代价地图时,可能会遇到各种编译和运行错误。本篇博客详细解析了在适配ROS Noetic过程中常见的报错及其解决方法,包括Boost文件结构更新、C++版本兼容性、Gazebo函数名变更、链接库缺失以及protobuf文件加载错误。通过逐步排查和修改,如更换头文件路径、调整C++标准、更新CMakeLists.txt文件等,成功解决了这些问题,使得`pgm_map_creator`功能包能够顺利编译和运行。

慕羽★的博客 2268

LeetCode 87. 扰乱字符串 python

87. 扰乱字符串 递归: class Solution: def isScramble(self, s1: str, s2: str) -> bool: if len(s1) != len(s2): return False if s1 == s2: return True if sorted(s1) != sorted(s2): return False

buglesxu的博客 267

2024年CSP-X复赛真题题解(T1:购物)

2024年CSP-X复赛真题题解(T1:购物)

王老师青少年编程 1195

【python/leetcode/M/87】Scramble String

递归法判断一种新定义的字符串

alicelmx的博客 712

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

LeetCode:87. Scramble String - Python

87. 扰乱字符串 问题描述: 给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树。 下图是字符串s1 = &quot;great&quot;的一种可能的表示形式。 在扰乱这个字符串的过程中,我们可以挑选任何一个非叶节点,然后交换它的两个子节点。 例如,如果我们挑选非叶节点&quot;gr&quot;,交换它的两个子节点,将会产生扰乱字符串&quot;rgeat&quot;。 我们将&quot;rgeat”称作&a

Growth Diary 754

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

【LeetCode】87. Scramble String 解题报告(Python)

题目分析: 这个题目比较难理解,说是给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树。下图是字符串 s1 = “great” 的一种可能的表示形式。 在扰乱这个字符串的过程中,我们可以挑选任何一个非叶节点,然后交换它的两个子节点。例如,如果我们挑选非叶节点 “gr” ,交换它的两个子节点,将会产生扰乱字符串 “rgeat” 。 我们将 "rgeat” 称作 ...

L141210113的专栏 412

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

python写算法题:leetcode: 87. Scramble String

class Solution(object): def isScramble(self, s1, s2): if sorted(s1) != sorted(s2): return False if len(s1)<4: return True """ :type s1: str :type s2: st...

247

[Leetcode]87. Scramble String

[Leetcode]87.Scramble String 本题难度: Hard Topic: divide and conquere Description Given a strings1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursivel...

weixin_30856965的博客 98

leetcode 87: Scramble String

leetcode 87: Scramble String java c++ python

西施豆腐渣 2285

[leetcode]87. Scramble String @python

题目Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = “great”: great / \ gr

qian2729的专栏 710

LeetCode开心刷题三十三天——87. Scramble String

87.Scramble String Hard 318521FavoriteShare Given a strings1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possibl...

weixin_30352191的博客 128

LeetCode 87. Scramble String

https://leetcode.com/problems/scramble-string/ Description Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible rep...

pku_Coder的博客 274

Leetcode 87. Scramble String

Problem We can scramble a string s to get a string t using the following algorithm: If the length of the string is 1, stop. If the length of the string is > 1, do the following: Split the string into two non-empty substrings at a random index, i.e., i

小白菜又菜 189

LeetCode - 解题笔记 - 87 - Scramble String

Solution 1 我自己的想了一个方法,可惜时间超了,只好参考别人的动态规划方法了。 先记一下我自己的方法。这个问题本身已经给了很充分的提示:回溯检查是否scramble。 最基础的scramble判定: 如果两个子串长度不对齐,肯定不会scramble 如果两个子串各个字符出现次数不对齐,肯定不会scramble 基于这个判定依据,整个思路就是递归地枚举所有的划分方案(简化成划分为等长度子串,但是因为能调换顺序,还得考虑一头一尾划分等长度判定),只要当前层次所有的枚举方案有一个方案是scrambl

支锦铭的博客 270

LeetCode-87 Scramble String

Description We can scramble a string s to get a string t using the following algorithm: If the length of the string is 1, stop. If the length of the string is > 1, do the following: • Split the string into two non-empty substrings at a random index, i.

秦智慧的博客 165

[LeetCode] 87. Scramble String_hard tag: 区间Dynamic Programming?

Given a strings1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation ofs1="great": great / \ ...

weixin_30260399的博客 88
上一篇: 200. Number of Islands Leetcode Python
下一篇: 201. Bitwise AND of Numbers Range Leetcode Python
hyperbolechi
博客等级 码龄12年 7粉丝 178原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址