LeetCode 87. Scramble String

HCIA重点知识 华为认证ICT工程师,是华为认证体系中用于标识个人能力在某一技术领域达到工程师级别的证明,表示通过认证的人员具备安装、配置、运行 ICT 设备,并进行基础故障排除的能力。抽象语言 -----> 编码 -----> 二进制 -----> 电信号 -----> 处理电信号信号的转换标准:协议,是一种允许电子设备相互交流的一系列标准的规则。即开放系统参考模型OSI/RM,简称“OSI”。OSI参考模型将计算机网络分为七个层次,自上而下依次为:应用层,表示层,会话层,传输层,网络层,数据链路层,物理层。1、应用层: 阅读详情

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 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.

Example 1:

Input: s1 = "great", s2 = "rgeat"
Output: true

Example 2:

Input: s1 = "abcde", s2 = "caebd"
Output: false

Code

class Solution:
    '''
    解题思路:
    对于字符串 s1 和 s2,设他们的长度为 L, 采用递归思想,如果
    1. s1 长度为 i 的前缀和 s2 长度为 i 的后缀构成 Scramble String;且s1 长为 (L-i) 的后缀 与 s2 长为 (L-i) 的前缀构成 Scramble String。(前缀后缀均非空)
    2. s1 长度为 i 的前缀和 s2 长度为 i 的前缀构成 Scramble String;且s1 长为 (L-i) 的后缀 与 s2 长为 (L-i) 的后缀构成 Scramble String。(前缀后缀均非空)
    满足以上两种情况之一的,则 s1, s2 构成 Scramble String。
    '''
    def isScramble(self, s1: 'str', s2: 'str') -> 'bool':
        len1 = len(s1)
        len2 = len(s2)
        
        if len1 != len2:
            return False

        if sorted(s1) != sorted(s2):
            return False
        
        # 长度小于3,一定成立
        if len1 <= 3 or s1 == s2:
            return True
        
        for i in range(1, len1):
            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
Git下载与安装 一、Git下载 二、Git安装 阅读详情

相关推荐

[Pytorch案例实践001]手写MNIST数字识别

Pytorch实现手写数字识别,带完整数据集。

2403_83044722的博客 1117

[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的博客 96

CSP-J 初赛模拟卷1 题解

只有一棵子树有后继,其他的都是叶结点才能保证树的深度最大,此时还剩(n-5)个结点,最后加上2,得到(n-3),即为答案。题解:先修改p的指针域,让它的左指针域指向q,然后再让q的右指针域赋值给p的右指针域,再然后修改q的右指针域使其指向p,最后需要改变的指针域就是原先q结点的后继节点的左指针域内容。设n个元素的入栈序列是1,2,3,...,n,出栈序列为p1,p2,...,pn,若p1=n,则pi(1≤i≤n)的值为(1)输入的三个数中,第二个数应是[0,11]上的整数,否则会出现数组越界的问题。

Sat77GryHp_Pro的博客 2805

LeetCode 87 scramble-string

LeetCode 87 一开始我的思路是把所有的scramble的strings都求出来,然后看看另外一个是否在里面。快速可以优化的地方就是记住已经求过的scramble的strings。 代码相对比较容易,每次把string都拆成两部分,然后求scramble的strings,再组合在一起: def isScrambleSlow(self, s1: str, s2: str) ->...

frankguodongchen的博客 181

家庭路由器从入门到精通:连接设置、优化排障全指南

路由器作为家庭网络的核心枢纽,其工作原理基于NAT(网络地址转换)和DHCP(动态主机配置协议)两大关键技术。NAT负责将公网IP转换为多个内网IP,实现多设备共享上网;DHCP则自动为接入设备分配IP地址,简化网络配置。这些技术共同构建了稳定、安全的局域网环境,其价值在于为智能家居、在线办公、高清影音和游戏娱乐等场景提供可靠的网络基础。理解路由器的物理连接、后台设置与故障排查逻辑,是优化Wi-Fi信号覆盖、提升网络性能、保障家庭网络安全的关键。本文以路由器配置与排障为核心,详细解析从WAN/LAN口区分、

weixin_33393017的博客 406

手把手推导APM的 sqrt_controller控制器

前言 玩过无人机的朋友对APM应该都不会陌生,如果是搞过多旋翼飞控且读过代码,肯定遇到过sqrt_controller控制器,它频繁的在位置、姿态控制中出现,所以非常有必要搞清楚这个控制原理。 下面以高度控制器为例来分析。 高度控制 APM的高度控制采用PID串级3环控制,即外环位置控制,中间速度控制,内环速度控制,如下图所示。 今天要讨论的sqrt_controller就是应用于位置控制器中。位置控制器的输出是速度控制器的参考reference输入,如果采用单P控制,其输出vel_ref = pos_e

周小逗的博客 3032

LeetCode87 Scramble String

LeetCode87 Scramble String问题来源LeetCode87问题描述 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 representat

yanqinghe123的专栏 411

ESP32语音交互实战:离线唤醒与DeepSeek大模型集成

本文详细介绍了如何将ESP32-S3开发板与天问ASRPRO语音模块结合,实现离线语音唤醒并集成DeepSeek大模型的智能交互系统。通过串口通信与API调用,系统可处理本地指令及复杂语义请求,适用于低成本、高隐私保护的AIoT语音交互场景。

rust6ferris的博客 873

Leetcode 87 Scramble String

思路:我成功地写出了如果只修改一次的话,判断是否是scramble string的方法。但很可惜,这边修改的节点可以是多个,我觉得我这种方法继续写下去应该是可以实现题目要求的,但是我想了好久,不会哈哈哈。我猜应该用recursive的方法来继续写下去?我也不太确定。但是我真的是太不喜欢recursive方法,哎但是,好像dfs,dp都和recursion有着一点关系。真得好好练练。我看了high votes answer,发现人家也是recursive的方法。真的我看懂他这个代码都花了一个多小时吧。烦死了.

GoodJobJasper的博客 266

如何在R语言中使用Logistic回归模型

在日常学习或工作中经常会使用线性回归模型对某一事物进行预测,例如预测房价、身高、GDP、学生成绩等,发现这些被预测的变量都属于连续型变量。然而有些情况下,被预测变量可能是二元变量,即成功或失败、流失或不流失、涨或跌等,对于这类问题,线性回归将束手无策。这个时候就需要另一种回归方法进行预测,即Logistic回归。 在实际应用中,Logistic模型主要有三大用途: 1)寻找危险因素,找到某些影响...

weixin_34056162的博客 9552

带有HSE组件的S32系列芯片中各子系统如何依次启动?

仔细阐述了上电后各个子系统的启动顺序,并给出了详细的启动步骤流程图。

车载系统攻城狮 1704

leetcode 87. Scramble String 二分法

leetcode 87. Scramble String public class Solution { public static void main(String[] args){ String s1 = "ab"; String s2 = "ba"; Solution s = new Solution(); System.out.println(s.isScr

独钓寒江雪 384

qt医疗信息系统(HIS)

**UI 组件**: QWidget、QTextBrowser、QTableWidget、QGraphicsView 等。- **网络通信**: Qt Network (TCP/IP)- **DICOM 处理**: DCMTK 3.6.7。- **数据库**: SQLite (轻量、跨平台)**数据库文件**: `hospital.db`- **开发框架**: Qt 6 (C++)- 医疗影像 (DICOM) 查看器。**医疗影像 (DICOM) 查看器****电子病历 (EMR) 浏览器**

寒寒豆豆软件公司的博客 340

LeetCode: 87. Scramble String

LeetCode: 87. Scramble String 题目描述 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 =...

杨领well的专栏 844

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

LeetCode87——Scramble String

LeetCode87——Scramble String 目前递归解决的方法

上善若水 537

NTscan扫描工具

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

【Leetcode】87. Scramble String

题目地址: https://leetcode.com/problems/scramble-string/ 给定两个字符串s1s_1s1​和s2s_2s2​,允许将s1s_1s1​做以下操作: 1、若s1=x+ys_1=x+ys1​=x+y,xxx和yyy都是s1s_1s1​的非空子串,那么可以将s1s_1s1​变成y+xy+xy+x; 2、在1的基础上,对xxx和yyy继续做与111相同操作。也就是说,一旦进行了1的操作,还可以继续将xxx和yyy重新视为s1s_1s1​来做分割交换的操作。 问s1s_1s

数学、算法爱好者的博客 219

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

[leetcode] 87. 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 representation of s1 = “great”: great / gr ...

Learning from the mistakes 285

【To Understand !!! DP or 递归】LeetCode 87. Scramble String

LeetCode 87. Scramble String 参考链接:[1]https://blog.csdn.net/makuiyu/article/details/44926439 [2]http://www.cnblogs.com/grandyang/p/4318500.html Solution1: 递归 class Solution { public: bool isSc...

Allenlzcoder的博客 310

[LeetCode]87. Scramble String

[LeetCode]87. Scramble String题目描述思路对于题目中描述的扰乱字符串,可能存在多个递归解决 当前输入的两个字符串如果相等,即返回true 否则 对i从1到len-1 判断 s1(0,i) 与 s2(0,i)或则s2(len - i, len - 1)是否满足扰乱串的条件 s1(i,len - 1) 与 s2(i, len - 1)或则s2(0, l

Lcharon的博客 748

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

hyperbolechi的专栏 845

LeetCode String 87 Scramble String

87. Scramble String 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”: To scramb...

Yi Jiao's Blog 164

leetcode 87: Scramble String

leetcode 87: Scramble String java c++ python

西施豆腐渣 2285

【一天一道LeetCode】#87. Scramble String

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substr

ZeeCoder 2948

[LeetCode] 87. Scramble String

[LeetCode] 87. Scramble StringGiven 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”:

ouwenjie10的专栏 259

leetcode-87. Scramble String

leetcode-87. Scramble String题目: 这里的基本方法就是累积字符数量, 然后对不同的位置做切分去找有没有满足条件的,如果有则直接返回。 从方法上来说其实综合了动态规划,hash表和DFT的方法是很好的综合性题目

千念飞羽的专栏 409

【LeetCode】87. Scramble String【分治法】

把一个字符串变成一颗二叉树,如果一个新字符串能由任意交换子树得到,则称为Scramble String。苦思冥想,自知不善于分治,故特别记录下来。 如果两个字符串是Scramble String,则必然有它们的两个子串分别互为Scramble String,如此二分下去直到字串长度为1,就只需判断是否相等。能有这种分而治之的思想此题就非常简单了。 for (int i = 1; i <...

zhaoqinmuxue的博客 247
上一篇: Inplace ABNSync 与 pytorch GPU多卡并行的一点坑
下一篇: LeetCode 84. Largest Rectangle in Histogram
PKU_CXK
博客等级 码龄10年 87粉丝 142原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值