leetcode 97. Interleaving String

图解LeetCode97:Interleaving String 文章目录理解题意解决方法一:最简单解法解决方法二:动态规划代码实现总结 Leetcode算法系列将详细讲解一些经典的面试算法题。 今天的题目是个人非常喜欢的一个,题目的名字叫做“Interleaving String”,这个题目是什么意思呢?   理解题意 Interleaving 从字面上讲是交错交叉的意思,给定两个字符串s1:"a"和s2:“b”,那么对于s3:"ab"我们说s3是s... 阅读详情

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.

Example 1:

Input: s1 = “aabcc”, s2 = “dbbca”, s3 = “aadbbcbcac”
Output: true
Example 2:

Input: s1 = “aabcc”, s2 = “dbbca”, s3 = “aadbbbaccc”
Output: false

问s3是否是s1和s2交叉组成的

交叉组成的条件:
s3的长度是s1,s2的长度和
字符必须满足s1,s2中的顺序,比如例2,不能保证c在a前面,所以是false

思路:

(1)二维dp

    public boolean isInterleave(String s1, String s2, String s3) {
        int length1 = s1.length();
        int length2 = s2.length();
        int length3 = s3.length();
        if (length1 + length2 != length3){
            return false;
        }
        //声明一个二维数组dp[][];其中dp[i][j]表示s1在第个位置,s2在第j个位置,s3在遍历第i+j个位置。dp[0][0]为s1和s2都为空
        boolean[][] dp = new boolean[length1 + 1][length2 + 1];
        //当s1不是empty,s2为empty时。只需比较当前遍历的位置s1的字串和s3的字串是否相等
        for (int i = 0; i <= length1; i++){
            dp[i][0] = s1.substring(0, i).equals(s3.substring(0, i)) ? true : false;
        }
        //当s2不是empty,s1是empty时
        for (int j = 0; j <= length2; j++){
            dp[0][j] = s2.substring(0, j).equals(s3.substring(0, j)) ? true : false;
        }
        //当s1和s2都不是空时。有两种情况,一种是从(i-1,j)到达(i,j)位置,如果(i-1,j)已经是交叉的了,只需比较s1的第i个位置的元素与s3的第i+j个位置的元素是否相等;另一种情况同理。
        for (int i = 1; i <= length1; i++){
            for (int j = 1; j <= length2; j++){
                dp[i][j] = (dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1)) ||
                            (dp[i][j-1] && s2.charAt(j - 1) == s3.charAt(i + j -1));
            }
        }
        return dp[s1.length()][s2.length()];

    }

(2)一维dp

别再混淆了!一张图看懂SAP特殊采购类30/40/70/80的区别与选型指南 本文深度解析SAP特殊采购类30/40/70/80的核心逻辑与业务影响,通过对比框架和真实案例帮助用户掌握其适用边界。重点介绍委外加工(类型30)、工厂间转移(类型40/70)及计划与执行分离(类型80)的配置要点与常见问题,提供选型决策树和实施检查清单,助力企业优化供应链管理。 阅读详情

相关推荐

高德地图可视化入门:零基础用Python+高德API打造你的第一个热力图

本文详细介绍了如何使用Python和高德地图API从零构建商业级热力图。通过环境配置、地理编码、数据清洗到最终可视化展示的全流程指南,帮助开发者快速掌握地图可视化技术,特别适合展示商圈人流、疫情热点等地理分布数据。

weixin_29053067的博客 307

Leetcode-97 Interleaving String

Leetcode-97 Interleaving StringquestionGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example, Given: s1 = “aabcc”, s2 = “dbbca”,When s3 = “aadbbcbcac”, return true

zhangwj0101的博客 298

手把手教你用FFmpeg实操:H.264转码、AVS2编码与H.265/HEVC参数调优避坑指南

本文详细解析了使用FFmpeg进行H.264转码、AVS2编码与H.265/HEVC参数调优的实战技巧。通过具体命令示例和参数详解,帮助开发者避开常见编码陷阱,提升视频处理效率和质量,特别适合音视频开发者和内容创作者参考。

weixin_30889885的博客 486

LeetCode力扣之97. Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false.packag...

LeeWei 344

点云欧式聚类快速了解

一种基于欧氏距离度量的聚类算法基于KD-Tree的近邻查询算法是加速欧式聚类算法的重要预处理方法KD-Tree是由二分搜索树演变而来的用于大规模高维度数据查找场景当中的索引结构,主要用于最近邻查找和近似最近邻查找。将整个空间划分为二叉树的形式,方便有效找到最近邻点云处理算法库PCL,是比较重要的,在后续会继续学习,然后发出更优质的文章。

xiaobaiwsc的博客 1724

C++ Qt6 qml CMakeLists.txt如何添加qrc文件

本文介绍了在Qt6项目中如何通过CMake管理QML图片资源。相比Qt5的qmake,CMake需要手动创建qrc文件并配置CMakeLists.txt。关键步骤包括:1)创建resources.qrc文件并添加图片资源;2)在CMakeLists.txt中设置CMAKE_AUTORCC为ON开启自动资源处理;3)使用qt_add_resources将qrc文件绑定到项目。配置完成后,可通过"qrc:alias"方式引用资源图片,解决了相对路径管理复杂的问题。

琅嬛福地 1455

LeetCode97—Interleaving String

LeetCode97—Interleaving String

上善若水 813

【数据集】1980-2020年(5年)土地利用分类数据-中国科学院

简介1980-2020年每五年土地利用数据-中国科学院

WW、forever的博客 3936

LeetCode 97. Interleaving String

97. Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example, Given: s1 = “aabcc”, s2 = “dbbca”,When s3 = “aadbbcbcac”, return true. When s3 = “aa

mingjc的博客 399

基于STM32的智能电子秤设计毕业论文

摘要:本文设计了一款基于STM32F103C8T6的智能电子秤系统,通过HX711称重传感器实现高精度测量,采用OLED显示和矩阵键盘完成人机交互。系统包含重量采集、数据处理、单价设置、去皮清零及总价计算等功能模块,经测试测量误差小于0.1%,稳定性良好。研究实现了低成本、高可靠性的智能称重方案,为后续无线传输和AI算法集成奠定了基础。关键词:STM32;智能电子秤;HX711;OLED显示;矩阵键盘。

3254968421的博客 851

【C语言】字符常量详解

字符常量是C语言中处理字符数据的重要工具。通过单引号括起一个字符,我们可以方便地使用字符常量进行字符判断、字符运算和字符串处理等操作。理解字符常量的表示方法、使用场景和ASCII码对应关系,对于编写高效的C语言程序至关重要。

LuckiBit 5022

Leetcode 97. Interleaving String

Leetcode 97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = &amp;quot;aabcc&amp;quot;, s2 = &amp;quot;dbbca&amp;quot;, s3 = &amp;quot;aadbbcbcac&amp;quot; Out

wyxwyx469410930的博客 405

Dify平台隐患url:/console/api/remote-files/upload暴露高危漏洞,配置nginx htpasswd模块认证机制屏蔽解决

内部网络探测: 攻击者可通过SSRF扫描内网服务云元数据访问: 可能访问AWS/Aliyun/腾讯云等元数据服务服务端请求伪造: 以服务器身份发起任意HTTP请求。

CN_Eden 1325

LeetCode 97. Interleaving String

题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbba

xclfafa的博客 385

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Leetcode 97 Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", ret

无名山丘,崛起成峰 1万+

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

[leetcode] 97 Interleaving String

问题描述: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc

追梦船的专栏 583

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

LeetCode 97. Interleaving String 题解

LeetCode 97. Interleaving String 题解题目描述 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aad

Guo15331092的博客 461

[LeetCode]97.Interleaving String

题目Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example, Given: s1 = “aabcc”, s2 = “dbbca”,When s3 = “aadbbcbcac”, return true. When s3 = “aadbbbaccc”, return fal

SmartSi 2387

【Leetcode】97. Interleaving String

题目地址: https://leetcode.com/problems/interleaving-string/ 给定三个字符串s1s_1s1​,s2s_2s2​和s3s_3s3​,则s1s_1s1​和s2s_2s2​可以通过交错的方式生成一个新字符串s′s's′,”交错的方式“的含义是,可以以插空的方式,将s1s_1s1​中的字符插到s2s_2s2​的字符之间,生成一个新的字符串,但s1s_1s1​的各个字符在新字符串中的相对顺序不能变。问s3s_3s3​能否由s1s_1s1​和s2s_2s2​这样交错生

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

leetcode【97】Interleaving String

问题描述 Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2. 题目意思就是给定三个字符串s1,s2和s3,判断s3能否由s1,s2交叉组成。 思路分析 用一个动态规划的方法,顶一个二维的bool类型的数组dp,dp[i][j]表示s3的前i+j个字符能否由s1的前i个字符和s2的前j...

fanyuwgy的博客 219

leetcode 97 Interleaving String(python)

python leetcode 97 Interleaving String

快递小可的博客 1334

LeetCode 97(Interleaving String)Java

原题:Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. 给定3个字符串,判断s3是否可以由s1和s2交错组成; 例如:s1="aa",s2="bc",s3="abca";s3可以由s1和s2交错组成; 思路:很多字符串问题某一位置的解会与它上一位置的解有关,这种情

Jassy1994的博客 582

LeetCode 97 Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", retur

mlweixiao 1381

leetcode 97. Interleaving String c++

97.Interleaving String Hard 72735FavoriteShare Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2. Example 1: Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" Outp...

cmy203的博客 258

[leetcode 97]Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", r

ER_Plough的专栏 584
上一篇: leetCode 95. Unique Binary Search Trees II(独特的二叉搜索树II)
下一篇: leetcode 115. Distinct Subsequences
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值