{"id":101,"date":"2017-07-27T19:01:00","date_gmt":"2017-07-27T11:01:00","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=101"},"modified":"2018-11-27T16:09:22","modified_gmt":"2018-11-27T08:09:22","slug":"14-longest-common-prefix","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=101","title":{"rendered":"14. Longest Common Prefix"},"content":{"rendered":"<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><strong>Input: <\/strong>[\"flower\",\"flow\",\"flight\"]\r\n<strong>Output:<\/strong> \"fl\"\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre><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<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def longestCommonPrefix(self, strs):\r\n        &quot;&quot;&quot;\r\n        :type strs: List[str]\r\n        :rtype: str\r\n        &quot;&quot;&quot;\r\n        result_str = &quot;&quot;\r\n        if strs is not None and len(strs) &gt; 0:\r\n            strs.sort()\r\n            str_first = strs[0]\r\n            str_last = strs[len(strs)-1]\r\n            a = 0\r\n            for i in str_first:\r\n                if i == str_last[a]:\r\n                    result_str += i\r\n                    a += 1\r\n                else:\r\n                    return result_str\r\n        return result_str\r\n<\/pre>\n<p><strong>csharp<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\"> \r\npublic class Solution {\r\n    public string LongestCommonPrefix(string[] strs) {\r\n        string result_str = &quot;&quot;;\r\n            if (strs != null &amp;&amp; strs.Length &gt; 0)\r\n            {\r\n                Array.Sort(strs);\r\n                string str_first = strs[0];\r\n                string str_last = strs[strs.Length - 1];\r\n                int a = 0;\r\n                foreach(var i in str_first)\r\n                {\r\n                    if (string.Equals(i,str_last[a]))\r\n                    {\r\n                        result_str += i;\r\n                        a++;\r\n\r\n                    }\r\n                    else\r\n                    {\r\n                        return result_str;\r\n                    }\r\n                }\r\n\r\n            }\r\n\r\n            return result_str;\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Write a function to find the longest common prefix stri<a href=\"https:\/\/www.ccagml.com\/?p=101\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">14. Longest Common Prefix<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/101"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=101"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions\/102"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}