{"id":2020,"date":"2018-03-07T23:46:22","date_gmt":"2018-03-08T07:46:22","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2020"},"modified":"2018-03-07T23:49:36","modified_gmt":"2018-03-08T07:49:36","slug":"leetcode-521-longest-uncommon-subsequence-i","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-521-longest-uncommon-subsequence-i\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 521. Longest Uncommon Subsequence I"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u6c422\u4e2a\u5b57\u7b26\u4e32\u6700\u957f\u7684\u4e0d\u540c\u5b50\u5e8f\u5217\u7684\u957f\u5ea6\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/longest-uncommon-subsequence-i\/description\/\">https:\/\/leetcode.com\/problems\/longest-uncommon-subsequence-i\/description\/<\/a><\/p>\n<p>Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be\u00a0<b>any\u00a0<\/b>subsequence of the other strings.<\/p>\n<p>A\u00a0<b>subsequence<\/b>\u00a0is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.<\/p>\n<p>The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn&#8217;t exist, return -1.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: \"aba\", \"cdc\"\r\nOutput: 3\r\nExplanation: The longest uncommon subsequence is \"aba\" (or \"cdc\"), \r\nbecause \"aba\" is a subsequence of \"aba\", \r\nbut not a subsequence of any other strings in the group of two strings. \r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>Both strings&#8217; lengths will not exceed 100.<\/li>\n<li>Only letters from a ~ z will appear in input strings.<\/li>\n<\/ol>\n<p><strong>Idea:<\/strong><\/p>\n<p>If two strings are the same, then the longest uncommon sequence does not exist, return -1.<\/p>\n<p>e.g. aaa vs aaa, return -1<\/p>\n<p>Otherwise, the longer string is always a uncommon sequence of the shorter one.<\/p>\n<p>e.g. aaab vs aaa, return 4<\/p>\n<p><strong>Solution 1:<\/strong><\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 3 ms\r\nclass Solution {\r\npublic:\r\n  int findLUSlength(string a, string b) {\r\n    return a == b ? -1 : max(a.length(), b.length());\r\n  }\r\n};<\/pre>\n<p>Java<\/p>\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 3 ms\r\nclass Solution {\r\n  public int findLUSlength(String a, String b) {\r\n    return a.equals(b) ? -1 : Math.max(a.length(), b.length());\r\n  }\r\n}<\/pre>\n<p>Python3<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 40 ms (beats 100%)\r\nWebsite: http:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-521-longest-uncommon-subsequence-i\/\r\n\"\"\"\r\nclass Solution:\r\n  def findLUSlength(self, a, b):\r\n    la, lb = len(a), len(b)\r\n    if la != lb: return max(la, lb)\r\n    return -1 if a == b else la\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u6c422\u4e2a\u5b57\u7b26\u4e32\u6700\u957f\u7684\u4e0d\u540c\u5b50\u5e8f\u5217\u7684\u957f\u5ea6\u3002 Problem: https:\/\/leetcode.com\/problems\/longest-uncommon-subsequence-i\/description\/ Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222],"class_list":["post-2020","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2020","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=2020"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2020\/revisions"}],"predecessor-version":[{"id":2023,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2020\/revisions\/2023"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}