首页 > 其他分享 >Leetcode 1143. Longest Common Subsequence

Leetcode 1143. Longest Common Subsequence

时间:2024-07-05 22:02:29浏览次数:13  
标签:1143 Subsequence text2 subsequence text1 slen1 Common slen2 dp

Problem

Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.

A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

  • For example, “ace” is a subsequence of “abcde”.

A common subsequence of two strings is a subsequence that is common to both strings.

Algorithm

Dynamic Programming (DP). Define state f(i, j) is the common subsequence of text1[0…i] and text2[0…j], so we have
f ( i , j ) = { f ( i − 1 , j − 1 ) + 1 if  s [ l e f t ] = = s [ r i g h t ] max ⁡ [ f ( i − 1 , j ) , f ( i , j − 1 ) ] otherwise f(i, j) = \begin{cases} f(i-1, j-1) + 1 & \text{if } s[left] == s[right ]\\ \max[f(i-1, j), f(i, j-1)] & \text{otherwise} \end{cases} f(i,j)={f(i−1,j−1)+1max[f(i−1,j),f(i,j−1)]​if s[left]==s[right]otherwise​

Code

class Solution:
    def longestCommonSubsequence(self, text1: str, text2: str) -> int:
        slen1 = len(text1)
        slen2 = len(text2)
        if not slen1 or not slen2:
            return 0
        
        dp = [[0] * (slen2 + 1) for i in range(slen1 + 1)]
        for i in range(1, slen1 + 1):
            for j in range(1, slen2 + 1):
                if text1[i - 1] == text2[j - 1]:
                    dp[i][j] = dp[i-1][j-1] + 1
                else:
                    dp[i][j] = max(dp[i-1][j], dp[i][j-1])
        
        return dp[slen1][slen2]

标签:1143,Subsequence,text2,subsequence,text1,slen1,Common,slen2,dp
From: https://blog.csdn.net/mobius_strip/article/details/140218852

相关文章

  • 代码随想录算法训练营第五十天 | 1143.最长公共子序列 392.判断子序列
    1143.最长公共子序列题目链接文章讲解视频讲解dp[i][j]:表示以text1以i-1为结尾text2以j-1为结尾的最长公共子序列为dp[i][j]递推公式:如果text1[i-1]==text2[j-1]那么dp[i][j]=dp[i-1][j-1]+1;  如果不相同的话,那么dp[i][j]=max(dp[i-1][j],dp[i][j-1]);cl......
  • 自动测试脚本----项目文件结构介绍(common,Data,Log,Case)
    一、总体框架介绍我们先看一下一般项目的总体结构: Automation:项目工程文件Common:存放一些封装的公共函数,可在已添加的py文件中追加函数和类,也可新增py文件和包来新增公共函数。新增包时请注意不要新增文件夹,新增文件夹的话,在文件夹下新增的py文件无法import,能impor......
  • 打包警告:chunk common [mini-css-extract-plugin]Conflicting order between:
    1.问题webpage5打包告警:chunkcommon[mini-css-extract-plugin]Conflictingorderbetween:2.解决方案:-vue.config.js配置//vue.config.jsmodule.exports={//...,css:{extract:{ignoreOrder:true},}};或者:调整组件引入的顺序3.......
  • 代码随想录算法训练营第50天 | 1143.最长公共子序列 、1035.不相交的线 、53. 最大子
    这几题都挺类似,都是求最长公共子序列,有些题目稍微变了下1143.最长公共子序列体会一下本题和718.最长重复子数组的区别视频讲解:https://www.bilibili.com/video/BV1ye4y1L7CQhttps://programmercarl.com/1143.最长公共子序列.html/***@param{string}text1*@param{......
  • Vite 打包如何允许存在commonjs
    首先安装依赖:npminstall@rollup/plugin-commonjs如图所示,添加plugin插件 示例代码:import{resolve}from'path';import{defineConfig}from'vite';importvuefrom'@vitejs/plugin-vue';importvueJsxfrom'@vitejs/plugin-vue-jsx&......
  • x-s、x-t、x-s-common、x-b3-traceid 签名算法分析记录(2024/6/20)
    【作者主页】:小鱼神1024【擅长领域】:JS逆向、小程序逆向、AST还原、验证码突防、Python开发、浏览器插件开发、React前端开发、NestJS后端开发等等【学习交流】:知识星球:https://t.zsxq.com/gkn0r;vx:studypy1024本文章中所有内容仅供学习交流使用,不用于其他任何目的,不提......
  • 记录一次代码中的ForkJoinPool.getCommonPoolParallelism()
    @Configuration@Slf4jpublicclassThreadPoolConfig{privatestaticfinalintCORE_POOL_SIZE=6;privatestaticfinalintMAX_POOL_SIZE=12;privatestaticfinalintKEEP_ALIVE_TIME=60;privatestaticfinalintQUEUE_CAPACITY=......
  • Windows中在commond如何设置系统环境变量
    最近测试项目中需要配置一个python环境用来发workjob,配置过程中有一个步骤需要增加系统变量:addtwosystemenvvarsforthetestapplicationbydifferentenvironments(dev/stg/prod):FORGE_TEST_CLIENT_IDFORGE_TEST_CLIENT_SECRET处理方法:1、查看已经设置了哪......
  • cc2/4链:针对commons-collections4的攻击
    cc2/4是干嘛的cc2、cc4针对的commons-collections4版本大于4.0(含)入口略有不同,后续和cc3一样通过TemplatesImpl加载恶意字节码调用链PriorityQueue.readobjectPriorityQueue的反序列化方法调用了heapify()heapifyheapify()调用了siftDown可以看见元素需要大于两个,所以我们......
  • 重学java 70.IO流 Commons-io工具包
    所有人都不看好你,可你偏偏最争气                            ——24.6.14一、介绍        IO技术开发中,代码量很大,而且代码的重复率较高。如果我们要遍历目录,拷贝自录就需要使用方法的递归调用,也增大了程序的复......