{"id":4045,"date":"2018-09-19T08:59:33","date_gmt":"2018-09-19T15:59:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4045"},"modified":"2018-09-19T08:59:41","modified_gmt":"2018-09-19T15:59:41","slug":"leetcode-14-longest-common-prefix","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-14-longest-common-prefix\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 14. Longest Common Prefix"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Write a function to find the longest common prefix string amongst an array of strings.<\/p>\n<p>If there is no common prefix, return an empty string\u00a0<code>\"\"<\/code>.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>[\"flower\",\"flow\",\"flight\"]\r\n<strong>Output:<\/strong> \"fl\"\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>[\"dog\",\"racecar\",\"car\"]\r\n<strong>Output:<\/strong> \"\"\r\n<strong>Explanation:<\/strong> There is no common prefix among the input strings.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<p>All given inputs are in lowercase letters\u00a0<code>a-z<\/code>.<\/p>\n<h1><strong>Solution: Brute Force<\/strong><\/h1>\n<p>Time complexity: O(mk), where k the length of common prefix.<\/p>\n<p>Space complexity: O(k)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  string longestCommonPrefix(vector&lt;string&gt;&amp; strs) {\r\n    if (strs.empty()) return \"\";\r\n    string ans;\r\n    for (int i = 0; i &lt; strs[0].size(); ++i) {\r\n      for (const string&amp; s : strs)\r\n        if (s.length() &lt;= i || s[i] != strs[0][i]) return ans;        \r\n      ans += strs[0][i];\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<p>Time complexity: (mk + k^2)<\/p>\n<pre class=\"lang:java decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\n  public String longestCommonPrefix(String[] strs) {\r\n    if (strs.length == 0) return \"\";\r\n    String ans = new String();\r\n    for (int i = 0; i &lt; strs[0].length(); ++i) {\r\n      char c = strs[0].charAt(i);\r\n      for (String s : strs)\r\n        if (s.length() &lt;= i || s.charAt(i) != c) return ans;\r\n      ans += c;\r\n    }\r\n    return ans;\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \"># Author: Huahua\r\nclass Solution:\r\n  def longestCommonPrefix(self, strs):\r\n    if not strs: return \"\"\r\n    ans = \"\"\r\n    for i in range(len(strs[0])):\r\n      for s in strs:\r\n        if len(s) &lt;= i or s[i] != strs[0][i]: return ans\r\n      ans += strs[0][i]\r\n    return ans<\/pre>\n<p>&nbsp;<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty&#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":[97,4],"class_list":["post-4045","post","type-post","status-publish","format-standard","hentry","category-string","tag-prefix","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4045","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=4045"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4045\/revisions"}],"predecessor-version":[{"id":4047,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4045\/revisions\/4047"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}