当前位置:网站首页>Simulation volume leetcode [general] 1143 Longest common subsequence
Simulation volume leetcode [general] 1143 Longest common subsequence
2022-07-06 06:17:00 【Encounter simulation volume】
1143. Longest common subsequence
Given two strings text1 and text2, Returns the longest of these two strings Common subsequence The length of . If it doesn't exist Common subsequence , return 0 .
A string of Subsequence This is a new string : It is to delete some characters from the original string without changing the relative order of the characters ( You can also delete no characters ) After the composition of the new string .
for example ,“ace” yes “abcde” The subsequence , but “aec” No “abcde” The subsequence .
Two strings of Common subsequence Is the subsequence shared by these two strings .
Example 1:
Input :text1 = “abcde”, text2 = “ace”
Output :3
explain : The longest common subsequence is “ace” , Its length is 3 .
Example 2:
Input :text1 = “abc”, text2 = “abc”
Output :3
explain : The longest common subsequence is “abc” , Its length is 3 .
Example 3:
Input :text1 = “abc”, text2 = “def”
Output :0
explain : The two strings have no common subsequence , return 0 .
Tips :
1 <= text1.length, text2.length <= 1000
text1 and text2 It only consists of lowercase English characters .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/longest-common-subsequence
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
from leetcode_python.utils import *
class Solution:
def __init__(self):
pass
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
length1,length2 = len(text1),len(text2)
if 0==length1 and 0==length2:return 0
this_dp = [0]*(length1+1)
for to_word2 in range(length2+1):
last_dp = this_dp.copy()
for from_word1 in range(length1+1):
if 0==from_word1 or 0==to_word2:
this_dp[from_word1] = 0
elif text1[from_word1-1]==text2[to_word2-1]:
this_dp[from_word1] = last_dp[from_word1-1] + 1
else:
this_dp[from_word1] = max(this_dp[from_word1 - 1], last_dp[from_word1])
print(f'to w2[{
to_word2}-1], {
this_dp}')
return this_dp[-1]
def minDistance_583_ Deletion of two strings (self, word1: str, word2: str) -> int:
length1,length2 = len(word1),len(word2)
if 0==length1 and 0==length2:return 0
this_dp = [0]*(length1+1) # word1 Each letter of to word2 The number of times each letter needs to be deleted
for to_word2 in range(length2+1):
last_dp = this_dp.copy()
for from_word1 in range(length1+1):
if 0==from_word1 or 0==to_word2: # consider word1 Delete to 0 The situation of
this_dp[from_word1] = from_word1 + to_word2
elif word1[from_word1-1]==word2[to_word2-1]:
this_dp[from_word1] = last_dp[from_word1-1]
else:
this_dp[from_word1] = min(this_dp[from_word1 - 1], last_dp[from_word1]) + 1
print(f'to w2[{
to_word2}-1], {
this_dp}')
return this_dp[-1]
def test(data_test):
s = Solution()
return s.longestCommonSubsequence(*data_test)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
["abcde","ace"],
["abc","abc"],
["abc","def"],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions _ Paper blog -CSDN Blog
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
- Seven imperceptible truths in software testing
- 模拟卷Leetcode【普通】1091. 二进制矩阵中的最短路径
- 【C语言】字符串左旋
- Basic knowledge of error
- MySQL之基础知识
- Career advancement Guide: recommended books for people in big factories
- The latest 2022 review of "graph classification research"
- selenium源码通读·9 |DesiredCapabilities类分析
- Database - current read and snapshot read
- 对数据安全的思考(转载)
猜你喜欢

MFC关于长字符串unsigned char与CString转换及显示问题

What are the test sites for tunnel engineering?

Construction and integration of Zipkin and sleuth for call chain monitoring

Digital triangle model acwing 1015 Picking flowers

properties文件

Significance of unit testing

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

【无App Push 通用测试方案
![[postman] test script writing and assertion details](/img/65/6520fe78bb2b3ff99f16d09ea8c0d1.png)
[postman] test script writing and assertion details

LeetCode 739. 每日温度
随机推荐
Aike AI frontier promotion (2.13)
F - true liars (category and search set +dp)
GTSAM中ISAM2和IncrementalFixedLagSmoother说明
Linux regularly backs up MySQL database
properties文件
浅谈专项测试之弱网络测试
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
E - 食物链
联合索引的左匹配原则
JMeter做接口测试,如何提取登录Cookie
Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
[ram IP] introduction and experiment of ram IP core
P问题、NP问题、NPC问题、NP-hard问题详解
(中)苹果有开源,但又怎样呢?
MFC关于长字符串unsigned char与CString转换及显示问题
CoordinatorLayout+NestedScrollView+RecyclerView 上拉底部显示不全
数字三角形模型 AcWing 1015. 摘花生
【Postman】Monitors 监测API可定时周期运行
这些年用Keil遇到的坑
Career advancement Guide: recommended books for people in big factories