LeetCode 1143 - Longest Common Subsequence
Difficulty: medium
Similar problem: LeetCode 718 - Maximum Length of Repeated Subarray
Problem Description
English (Longest Common Subsequence)
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.
Example 1:
1 |
|
Example 2:
1 |
|
Example 3:
1 |
|
Constraints:
1 <= text1.length, text2.length <= 1000
text1
andtext2
consist of only lowercase English characters.
Chinese (最长公共子序列)
给定两个字符串 text1
和 text2
,返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列,返回 0
。
一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。
例如,"ace"
是 "abcde"
的子序列,但 "aec"
不是 "abcde"
的子序列。
两个字符串的 公共子序列 是这两个字符串所共同拥有的子序列。
示例 1:
1 |
|
示例 2:
1 |
|
示例 3:
1 |
|
提示:
1 <= text1.length, text2.length <= 1000
text1
和text2
仅由小写英文字符组成。
Solution
C++
1 |
|
Python
1 |
|
Advanced
It’s available to compress the dp array into one dimension.
LeetCode 1143 - Longest Common Subsequence
http://wasprime.github.io/Algorithm/LeetCode/DynamicProgramming/LeetCode-1143-Longest-Common-Subsequence/