{"id":1837,"date":"2018-02-20T23:33:36","date_gmt":"2018-02-21T07:33:36","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1837"},"modified":"2018-02-20T23:34:57","modified_gmt":"2018-02-21T07:34:57","slug":"leetcode-214-shortest-palindrome","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-214-shortest-palindrome\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 214. Shortest Palindrome"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f60\u53ef\u4ee5\u5728\u5b83\u7684\u524d\u9762\u6dfb\u52a0\u4e00\u4e9b\u5b57\u7b26\uff0c\u8fd4\u56de\u6700\u77ed\u7684\u56de\u6587\u5b57\u7b26\u4e32\u3002<\/p>\n<p>Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.<\/p>\n<p>For example:<\/p>\n<p>Given\u00a0<code>\"aacecaaa\"<\/code>, return\u00a0<code>\"aaacecaaa\"<\/code>.<\/p>\n<p>Given\u00a0<code>\"abcd\"<\/code>, return\u00a0<code>\"dcbabcd\"<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution 1: Brute Force<\/strong><\/p>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++ w\/ copy<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 102 ms (&lt;25.74%)\r\nclass Solution {\r\npublic:\r\n  string shortestPalindrome(string s) {\r\n    const string r(s.rbegin(), s.rend());\r\n    const int n = s.size();\r\n    for (int i = 0; i &lt; n; ++i)\r\n      if (s.substr(0, n - i) == r.substr(i))\r\n        return r.substr(0, i) + s;\r\n    return \"\";\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>C++ w\/o copy<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 13 ms (&lt;49.74%)\r\nclass Solution {\r\npublic:\r\n  string shortestPalindrome(string s) {\r\n    const string r(s.rbegin(), s.rend());\r\n    const int n = s.size();\r\n    for (int i = 0; i &lt; n; ++i)      \r\n      if (s.compare(0, n - i, r, i, n - i) == 0)\r\n        return r.substr(0, i) + s;\r\n    return \"\";\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f60\u53ef\u4ee5\u5728\u5b83\u7684\u524d\u9762\u6dfb\u52a0\u4e00\u4e9b\u5b57\u7b26\uff0c\u8fd4\u56de\u6700\u77ed\u7684\u56de\u6587\u5b57\u7b26\u4e32\u3002 Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,47],"tags":[217],"class_list":["post-1837","post","type-post","status-publish","format-standard","hentry","category-greedy","category-string","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1837","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=1837"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1837\/revisions"}],"predecessor-version":[{"id":1841,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1837\/revisions\/1841"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}