1143. Longest Common Subsequence

link to problem

Description:

Given two strings text1 and text2, return the length of their longest common subsequence.

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. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.

If there is no common subsequence, return 0.

Solution:

 1 class Solution:
 2     def longestCommonSubsequence(self, text1: str, text2: str) -> int:
 3
 4         n1 = len(text1)
 5         n2 = len(text2)
 6
 7         dp = [[0 for i in range(n2+1)] for j in range(n1+1)]
 8         for i in range(n1):
 9             for j in range(n2):
10                 if text1[i]==text2[j]:
11                     dp[i][j] = 1 + dp[i-1][j-1]
12                 else:
13                     dp[i][j] = max(dp[i-1][j], dp[i][j-1])
14
15         return dp[n1-1][n2-1]

Notes:

2-d Dynamic Programming

O(mn)

原文地址:https://www.cnblogs.com/beatets/p/12154648.html

时间: 2024-10-09 00:16:28

1143. Longest Common Subsequence的相关文章

【leetcode】1143. Longest Common Subsequence

题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. 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

LeetCode 1143. Longest Common Subsequence

原题链接在这里:https://leetcode.com/problems/longest-common-subsequence/ 题目: Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some cha

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

Dynamic Programming | Set 4 (Longest Common Subsequence)

首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", "bdf", "aeg", '"acefg"等都是"abcdefg"的子序列.因此,一个长度为n的序列拥有2^n中可能的子序列(序列中的每一个元素只有选或者不选两种可能,因此是2^n). Example: LCS for inp

uva10405 - Longest Common Subsequence(LIS,最长共同自序列)

题目:uva10405 - Longest Common Subsequence(LIS,最长共同自序列) 题目大意:找出两个字符串中的最长公共的子序列. 解题思路:这类问题是第一次接触,不知道怎么做.百度了一下,发现了递推公式:dp[i][j]:代表第一个字符串的前i个字符和第二个字符串的前j个字符比较能得到的最长的公共子序列.s[i] == s[j] ,dp[i][j] = dp[i - 1][j - 1] + 1: s[i] != s[j] , dp[i][j] = Max (dp[i][

[HackerRank] The Longest Common Subsequence

This is the classic LCS problem. Since it requires you to print one longest common subsequence, just use the O(m*n)-space version here. My accepted code is as follows. 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5

[Algorithms] Longest Common Subsequence

The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the length of the longest sequence r, which is a subsequence of both s and t. Do you know the difference between substring and subequence? Well, substring i

HDU 2253 Longest Common Subsequence Again

其实这个题我还不会,学长给了一个代码交上去过了,据说用到了一种叫做位压缩的技术,先贴代码吧,以后看懂了再来写 #include <stdio.h> #include <string.h> #define M 30005 #define SIZE 128 #define WORDMAX 3200 #define BIT 32 char s1[M], s2[M]; int nword; unsigned int str[SIZE][WORDMAX]; unsigned int tmp1

uva 10405 Longest Common Subsequence (最长公共子序列)

uva 10405 Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences: abcdgh a