{"id":2050,"date":"2018-03-10T01:39:08","date_gmt":"2018-03-10T09:39:08","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2050"},"modified":"2018-03-10T01:39:18","modified_gmt":"2018-03-10T09:39:18","slug":"leetcode-647-palindromic-substrings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-647-palindromic-substrings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 647. Palindromic Substrings"},"content":{"rendered":"<p><strong>Problem:<\/strong><\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/palindromic-substrings\/description\/\">https:\/\/leetcode.com\/problems\/palindromic-substrings\/description\/<\/a><\/p>\n<p>Given a string, your task is to count how many palindromic substrings in this string.<\/p>\n<p>The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: \"abc\"\r\nOutput: 3\r\nExplanation: Three palindromic strings: \"a\", \"b\", \"c\".\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: \"aaa\"\r\nOutput: 6\r\nExplanation: Six palindromic strings: \"a\", \"a\", \"a\", \"aa\", \"aa\", \"aaa\".\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The input string length won&#8217;t exceed 1000.<\/li>\n<\/ol>\n<p><strong>Solution 1: Brute Force<\/strong><\/p>\n<p>Time complexity: O(n^2)<\/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: 5 ms\r\nclass Solution {\r\npublic:\r\n  int countSubstrings(string s) {\r\n    int ans = 0;\r\n    for (int i = 0; i &lt; s.length(); ++i) {\r\n      ans += count(s, i, i); \/\/ odd length\r\n      ans += count(s, i, i + 1); \/\/ even length\r\n    }\r\n    return ans;\r\n  }\r\nprivate:\r\n  int count(const string&amp; s, int l, int r) {\r\n    int ans = 0;\r\n    while (l &gt;= 0 &amp; r &lt; s.length() &amp;&amp; s[l--] == s[r++])\r\n      ++ans;\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: https:\/\/leetcode.com\/problems\/palindromic-substrings\/description\/ Given a string, your task is to count how many palindromic substrings in this string. The substrings with different start indexes or end&#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":[95],"class_list":["post-2050","post","type-post","status-publish","format-standard","hentry","category-string","tag-palindrome","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2050","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=2050"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2050\/revisions"}],"predecessor-version":[{"id":2052,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2050\/revisions\/2052"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}