{"id":3917,"date":"2018-09-12T00:45:12","date_gmt":"2018-09-12T07:45:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3917"},"modified":"2020-01-02T21:23:36","modified_gmt":"2020-01-03T05:23:36","slug":"leetcode-5-longest-palindromic-substring","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-5-longest-palindromic-substring\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 5. Longest Palindromic Substring"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a string&nbsp;<strong>s<\/strong>, find the longest palindromic substring in&nbsp;<strong>s<\/strong>. You may assume that the maximum length of&nbsp;<strong>s<\/strong>&nbsp;is 1000.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \"babad\"\n<strong>Output:<\/strong> \"bab\"\n<strong>Note:<\/strong> \"aba\" is also a valid answer.\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \"cbbd\"\n<strong>Output:<\/strong> \"bb\"\n<\/pre>\n<h1><strong>Solution: DP<\/strong><\/h1>\n<p>Try all possible i and find the longest palindromic string whose center is i (odd case) and i \/ i + 1 (even case).<\/p>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(1)<\/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, 16 ms, 8.7 MB\nclass Solution {\npublic:\n  string longestPalindrome(string s) {\n    const int n = s.length();\n    auto getLen = [&amp;](int l, int r) {\n      while (l &gt;= 0 &amp;&amp; r &lt; n \n             &amp;&amp; s[l] == s[r]) {\n        --l;\n        ++r;\n      }\n      return r - l - 1;\n    };\n    int len = 0;\n    int start = 0;\n    for (int i = 0; i &lt; n; ++i) {\n      int cur = max(getLen(i, i), \n                    getLen(i, i + 1));\n      if (cur &gt; len) {\n        len = cur;\n        start = i - (len - 1) \/ 2;\n      }\n    }\n    return s.substr(start, len);\n  }\n};\n\n<\/pre>\n<\/div><\/div>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:Java decode:true  \">\/\/ Author: Huahua, 6 ms\uff0c 36.5 MB\nclass Solution {\n  public String longestPalindrome(String s) {\n    int len = 0;\n    int start = 0;\n    for (int i = 0; i &lt; s.length(); ++i) {\n      int cur = Math.max(getLen(s, i, i), \n                         getLen(s, i, i + 1));\n      if (cur &gt; len) {\n        len = cur;\n        start = i - (cur - 1) \/ 2;\n      }\n    }\n    return s.substring(start, start + len);\n  }\n  \n  private int getLen(String s, int l, int r) {\n    while (l &gt;= 0 &amp;&amp; r &lt; s.length() \n           &amp;&amp; s.charAt(l) == s.charAt(r)) {\n      --l;\n      ++r;\n    }\n    return r - l - 1;\n  }\n}\n<\/pre>\n<\/div><\/div>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true  \"># Author: Huahua, 812 ms, 12.7MB\nclass Solution:\n  def longestPalindrome(self, s: str) -&gt; str:\n    n = len(s)\n    def getLen(l, r):\n      while l &gt;= 0 and r &lt; n and s[l] == s[r]:\n        l -= 1\n        r += 1\n      return r - l - 1\n    \n    start = 0\n    length = 0    \n    for i in range(n):      \n      cur = max(getLen(i, i),\n                getLen(i, i + 1))\n      if cur &lt;= length: continue\n      length = cur\n      start = i - (cur - 1) \/\/ 2\n    return s[start : start + length]\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a string&nbsp;s, find the longest palindromic substring in&nbsp;s. You may assume that the maximum length of&nbsp;s&nbsp;is 1000. Example 1: Input: &#8220;babad&#8221; Output: &#8220;bab&#8221;&#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,117,177,498,95,4],"class_list":["post-3917","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-longest","tag-medium","tag-on2","tag-palindrome","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3917","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=3917"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3917\/revisions"}],"predecessor-version":[{"id":6044,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3917\/revisions\/6044"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}