java.lang.Object
g1101_1200.s1143_longest_common_subsequence.Solution

public class Solution extends Object
1143 - Longest Common Subsequence.<p>Medium</p> <p>Given two strings <code>text1</code> and <code>text2</code>, return <em>the length of their longest <strong>common subsequence</strong>.</em> If there is no <strong>common subsequence</strong> , return <code>0</code>.</p> <p>A <strong>subsequence</strong> 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.</p> <ul> <li>For example, <code>&quot;ace&quot;</code> is a subsequence of <code>&quot;abcde&quot;</code>.</li> </ul> <p>A <strong>common subsequence</strong> of two strings is a subsequence that is common to both strings.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> text1 = &ldquo;abcde&rdquo;, text2 = &ldquo;ace&rdquo;</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The longest common subsequence is &ldquo;ace&rdquo; and its length is 3.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> text1 = &ldquo;abc&rdquo;, text2 = &ldquo;abc&rdquo;</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The longest common subsequence is &ldquo;abc&rdquo; and its length is 3.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> text1 = &ldquo;abc&rdquo;, text2 = &ldquo;def&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> There is no such common subsequence, so the result is 0.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= text1.length, text2.length <= 1000</code></li> <li><code>text1</code> and <code>text2</code> consist of only lowercase English characters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • longestCommonSubsequence

      public int longestCommonSubsequence(String text1, String text2)