{"id":5504,"date":"2019-08-29T23:24:35","date_gmt":"2019-08-30T06:24:35","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5504"},"modified":"2019-08-29T23:32:55","modified_gmt":"2019-08-30T06:32:55","slug":"leetcode-1143-longest-common-subsequence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1143-longest-common-subsequence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1143. Longest Common Subsequence"},"content":{"rendered":"\n<p>Given two strings&nbsp;<code>text1<\/code>&nbsp;and&nbsp;<code>text2<\/code>, return the length of their longest common subsequence.<\/p>\n\n\n\n<p>A&nbsp;<em>subsequence<\/em>&nbsp;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, &#8220;ace&#8221; is a subsequence of &#8220;abcde&#8221; while &#8220;aec&#8221; is not).&nbsp;A&nbsp;<em>common subsequence<\/em>&nbsp;of two strings is a subsequence that is common to both strings.<\/p>\n\n\n\n<p>If there is no common subsequence, return 0.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> text1 = \"abcde\", text2 = \"ace\" \n<strong>Output:<\/strong> 3  \n<strong>Explanation:<\/strong> The longest common subsequence is \"ace\" and its length is 3.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> text1 = \"abc\", text2 = \"abc\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The longest common subsequence is \"abc\" and its length is 3.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> text1 = \"abc\", text2 = \"def\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There is no such common subsequence, so the result is 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= text1.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= text2.length &lt;= 1000<\/code><\/li><li>The input strings consist of lowercase English characters only.<\/li><\/ul>\n\n\n\n<p><strong>Solution: DP<\/strong><\/p>\n\n\n\n<p>Use dp[i][j] to represent the length of longest common sub-sequence of text1[0:i] and text2[0:j]<br>dp[i][j] = dp[i &#8211; 1][j &#8211; 1]  + 1 if text1[i &#8211; 1] == text2[j &#8211; 1] else max(dp[i][j &#8211; 1], dp[i &#8211; 1][j])<\/p>\n\n\n\n<p>Time complexity: O(mn)<br>Space complexity: O(mn) -&gt; O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua, 16ms 14.9 MB\nclass Solution {\npublic:\n  int longestCommonSubsequence(string text1, string text2) {\n    int m = text1.length();\n    int n = text2.length();\n    vector<vector<int>> dp(m + 1, vector<int>(n + 1));\n    for (int i = 0; i < m; ++i)\n      for (int j = 0; j < n; ++j)\n        if (text1[i] == text2[j])\n          dp[i + 1][j + 1] = dp[i][j] + 1;\n        else\n          dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);      \n    return dp[m][n];\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++\/V2<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua, 8ms, 8.8MB\nclass Solution {\npublic:\n  int longestCommonSubsequence(string text1, string text2) {\n    int m = text1.length();\n    int n = text2.length();    \n    vector<int> dp(n + 1);    \n    for (int i = 0; i < m; ++i) {\n      int prev = 0; \/\/ dp[i][j]\n      for (int j = 0; j < n; ++j) {        \n        int curr = dp[j + 1]; \/\/ dp[i][j + 1]\n        if (text1[i] == text2[j])\n          \/\/ dp[i + 1][j + 1] = dp[i][j] + 1\n          dp[j + 1] = prev + 1; \n        else\n          \/\/ dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j])\n          dp[j + 1] = max(curr, dp[j]);\n        prev = curr;\n      }    \n    }    \n    return dp[n]; \/\/ dp[m][n]\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++\/V3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int longestCommonSubsequence(string text1, string text2) {\n    int m = text1.length();\n    int n = text2.length();    \n    vector<int> dp1(n + 1);\n    vector<int> dp2(n + 1);\n    for (int i = 0; i < m; ++i) {      \n      for (int j = 0; j < n; ++j)\n        if (text1[i] == text2[j])\n          dp2[j + 1] = dp1[j] + 1; \n        else          \n          dp2[j + 1] = max(dp1[j + 1], dp2[j]);              \n      swap(dp1, dp2);\n    }    \n    return dp1[n];\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given two strings&nbsp;text1&nbsp;and&nbsp;text2, return the length of their longest common subsequence. A&nbsp;subsequence&nbsp;of a string is a new string generated from the original string with some&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,26,66,177,366,4],"class_list":["post-5504","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-dynamic-programming","tag-lcs","tag-medium","tag-omn","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5504"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5504\/revisions"}],"predecessor-version":[{"id":5510,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5504\/revisions\/5510"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}