[leetcode] 87. Scramble String

【昇腾】用MindIE(服务化方式)拉起glm4-9b-chat模型问题解决 【昇腾】用MindIE拉起glm4-9b-chat模型问题解决: LLMInferEngine failed to init LLMInferModels ERR: Failed to init endpoint! Please check the service log or console output. Killed make sure the folder's owner has execute permission. 阅读详情

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”:
在这里插入图片描述
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

解法1:递归

如果s1和s2是 scramble 的话,那么必然存在一个在 s1 上的长度 l1,将 s1 分成 s11 和 s12 两段,同样有 s21 和 s22,那么要么 s11 和 s21 是 scramble 的并且 s12 和 s22 是 scramble 的;要么 s11 和 s22 是 scramble 的并且 s12 和 s21 是 scramble 的。

class Solution {
public:
    bool isScramble(string s1, string s2) {
        if(s1.size() != s2.size()) return false;
        if(s1 == s2) return true;
        string str1 = s1, str2 = s2;
        sort(str1.begin(), str1.end());
        sort(str2.begin(), str2.end());
        if(str1 != str2) return false;
        for(int i=1;i<str1.size();i++){
            string s11 = s1.substr(0,i);
            string s12 = s1.substr(i);
            string s21 = s2.substr(0, i);
            string s22 = s2.substr(i);
            if (isScramble(s11, s21) && isScramble(s12, s22)) return true;
            s21 = s2.substr(s1.size() - i);
            s22 = s2.substr(0, s1.size() - i);
            if (isScramble(s11, s21) && isScramble(s12, s22)) return true;
        }
        return false;
    } 
};

解法2:动态规划

维护一个三维数组dp[i][j][n],i是s1的起始字符,j是s2的起始字符,n是字符串长度
dp[i][j][n]表示s1以i开始,s2以j开始的长度为n的字符串是不是scramble。
判断这个是不是满足,其实我们首先是把当前 s1[i…i+len-1] 字符串劈一刀分成两部分,然后分两种情况:
第一种是左边和 s2[j…j+len-1] 左边部分是不是 scramble,以及右边和 s2[j…j+len-1] 右边部分是不是 scramble;
第二种情况是左边和 s2[j…j+len-1] 右边部分是不是 scramble,以及右边和 s2[j…j+len-1] 左边部分是不是 scramble。
如果以上两种情况有一种成立,说明 s1[i…i+len-1] 和 s2[j…j+len-1] 是 scramble 的。
而对于判断这些左右部分是不是 scramble 我们是有历史信息的,因为长度小于n的所有情况我们都在前面求解过了(也就是长度是最外层循环)。
上面说的是劈一刀的情况,对于 s1[i…i+len-1] 我们有 len-1 种劈法,在这些劈法中只要有一种成立,那么两个串就是 scramble 的。
总结起来递推式是 res[i][j][len] = || (res[i][j][k] && res[i+k][j+k][len-k] || res[i][j+len-k][k]&&res[i+k][j][len-k]) 对于所有 1<=k<len

class Solution {
public:
    bool isScramble(string s1, string s2) {
        if(s1.size() != s2.size()) return false;
        if(s1 == s2) return true;
        int n = s1.size();
        vector<vector<vector<bool> > > dp (n, vector<vector<bool> >(n, vector<bool>(n + 1, false)));
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                dp[i][j][1] = (s1[i] == s2[j]);
        
        for(int len=2;len<=n;len++)
            for(int i=0;i<=n-len;i++)
                for(int j=0;j<=n-len;j++)
                    for(int k=1;k<len;k++)
                        if((dp[i][j][k] && dp[i+k][j+k][len-k]) || (dp[i + k][j][len - k] && dp[i][j + len - k][k]))
                            dp[i][j][len] = true;
        return dp[0][0][n];
    } 
};

参考

https://www.cnblogs.com/grandyang/p/4318500.html

从DDPM到DDIM:深入解读《Denoising Diffusion Implicit Models》 DDIM发表在ICRL2021上,是DDPM重要的改进之一,能显著提高DDPM的样本质量、减少采样时间,并且能显式控制插值,已经被广泛应用到现在的Diffusion Models上。这篇博客和大家一起详细解读一下DDIM,认识这一伟大的模型。......... 阅读详情

相关推荐

保姆级教程:用中移ML307模组对接高德/OneOS定位服务,从配置apikey到解析+MLBSLOC状态码

本文详细介绍了如何使用中移ML307系列模组对接高德/OneOS定位服务,从配置apikey到解析+MLBSLOC状态码的全过程。涵盖高德定位服务和OneOS定位服务的配置步骤、常见错误处理及性能优化技巧,帮助开发者快速实现基站定位功能,提升物联网项目的定位精度和稳定性。

weixin_30632089的博客 505

[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

到底是什么决定了二极管的最高工作频率?

硬件工程师,应该都用过二极管的吧,不过也许有一个误区,大多数人可能并不知道,或者说是理解有问题,下面就来细细说下。 一个问题 先提一个问题:到底是什么决定了二极管的最高工作频率? 估计有不少人会回答是二极管反向恢复时间Trr,也有人会说是二极管结电容,那到底谁是对的呢?或者说都一样,反向恢复时间由结电容决定? 到底什么决定了二极管的最高工作频率,我们暂且不论,不过需要知道的是,二极管的反向恢复时间和结电容根本就是两回事,反向恢复时间绝不能等同于结电容的充放电时间。 为什么我这么说呢?.

weixin_42005993的博客 4831

LeetCode 87 scramble-string

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

frankguodongchen的博客 180

QMI8658A和QMC5883L传感器校准全攻略:从零偏校准到椭球拟合

本文详细解析了QMI8658A和QMC5883L传感器的校准方法,从零偏校准到椭球拟合,涵盖了9轴运动传感器的核心误差来源及补偿技术。通过实战案例和数学建模,帮助开发者提升传感器精度,适用于无人机、AR/VR等智能硬件应用。

weixin_29189381的博客 157

SAR成像概述

1. 发展概述 雷达是英文Radar(Radio detection and ranging)的音译,原意为“无线电探测与测距”。雷达的发明是无线电发展史上的里程碑,二战时,英国急需通过雷达探测空中金属物体来帮助搜寻德国飞机,这期间,雷达技术飞速发展,发展出了地对空、空对地轰炸、空对空火控、敌我识别等功能。雷达在第二次世界大战中发挥了重大作用,至今仍是军用和许多民用领域的重要传感器。 早期雷达系统通过时间延迟来测距,通过天线指向来测角,通过多普勒频移测速。1951年,Carl Wiley发现,通..

5068

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

【异常】Cursor IDE 扩展主机超时错误处理文档The agent execution provider did not respond within 4 seconds.

该错误通常是 Cursor IDE 的临时性问题,不影响项目代码。大多数情况下,重新加载窗口或重启 IDE 即可解决。如果问题持续,可按上述进阶方案排查。重新加载窗口(最快)重启 IDE检查系统资源清理缓存检查扩展更多关于 AI+教育领域的干货,可以关注我的微信公众号第一时间获取哦~

奶爸的编程之路,也就一周冷个三天 2697

Leetcode 87 Scramble String

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

GoodJobJasper的博客 266

【中项第三版】系统集成项目管理工程师 | 第 1 章 信息化发展

【中项第三版】系统集成项目管理工程师 | 第 1 章 信息化发展

静谧、淡雅 6574

09 SAP CPI 访问公共API接口

我在网上找了一个公共API,入参输入汉字返回拼音。至此,一个真正的访问API的接口就做好了。点击保存时可看到红叉报错。

SAP CPI 477

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

损失函数大汇总(六十一)(Signed Distance Map Loss附公式推导与代码)

摘要 本文介绍了Signed Distance Map (SDM) Loss,一种将形状先验直接融入分割任务的方法。SDM将每个像素表示为到目标边界的带符号距离(内部为负,外部为正),相比二值mask能更连续地表达形状几何信息。SDM Loss的核心思想是让网络学习这个隐式形状场,通过可微的Heaviside函数近似将SDM转换为分割结果。文中重点分析了一种product-based regression loss,它能同时关注距离数值和内外符号的正确性,对边界区域尤为敏感。SDM Loss通常与传统的分割

悦学共鸣,温柔以待,汇聚光芒,共同成长 186

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的专栏 843

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

LeetCode87——Scramble String

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

上善若水 537

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

【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

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

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【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

西施豆腐渣 2284

【一天一道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的博客 246

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 = "great": great / \ gr ...

CS番茄 289
上一篇: [leetcode] 221. Maximal Square && 85. Maximal Rectangle
下一篇: [leetcode] 97. Interleaving String
Peng_maple
博客等级 码龄10年 0粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值