-
Notifications
You must be signed in to change notification settings - Fork 160
/
longest-common-subsequence.md
41 lines (30 loc) · 1.55 KB
/
longest-common-subsequence.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<p>Given two strings <code>text1</code> and <code>text2</code>, return the length of their longest common subsequence.</p>
<p>A <em>subsequence</em> 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 <em>common subsequence</em> of two strings is a subsequence that is common to both strings.</p>
<p> </p>
<p>If there is no common subsequence, return 0.</p>
<p> </p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> text1 = "abcde", text2 = "ace"
<strong>Output:</strong> 3
<strong>Explanation:</strong> The longest common subsequence is "ace" and its length is 3.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> text1 = "abc", text2 = "abc"
<strong>Output:</strong> 3
<strong>Explanation:</strong> The longest common subsequence is "abc" and its length is 3.
</pre>
<p><strong>Example 3:</strong></p>
<pre>
<strong>Input:</strong> text1 = "abc", text2 = "def"
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no such common subsequence, so the result is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= text1.length <= 1000</code></li>
<li><code>1 <= text2.length <= 1000</code></li>
<li>The input strings consist of lowercase English characters only.</li>
</ul>