{"id":6127,"date":"2020-01-26T08:41:10","date_gmt":"2020-01-26T16:41:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6127"},"modified":"2020-01-26T08:42:54","modified_gmt":"2020-01-26T16:42:54","slug":"leetcode-1328-break-a-palindrome","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1328-break-a-palindrome\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1328. Break a Palindrome"},"content":{"rendered":"\n<p>Given a palindromic string&nbsp;<code>palindrome<\/code>, replace&nbsp;<strong>exactly one<\/strong>&nbsp;character by any lowercase English letter so that the string becomes the lexicographically smallest possible string that&nbsp;<strong>isn&#8217;t<\/strong>&nbsp;a palindrome.<\/p>\n\n\n\n<p>After doing so, return the final string.&nbsp; If there is no way to do so, return the empty string.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> palindrome = \"abccba\"\n<strong>Output:<\/strong> \"aaccba\"\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> palindrome = \"a\"\n<strong>Output:<\/strong> \"\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= palindrome.length &lt;= 1000<\/code><\/li><li><code>palindrome<\/code>&nbsp;consists of only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>For the first half of the string, replace the first non &#8216;a&#8217; character to &#8216;a&#8217;.<\/p>\n\n\n\n<p>e.g. abcdcba =&gt; aacdcba<\/p>\n\n\n\n<p>If not found which means the the entire string is &#8216;a&#8217; expect the middle one if the length is odd, like aa or aba, replace the last character to &#8216;b&#8217;.<\/p>\n\n\n\n<p>aa =&gt; ab<br>aba =&gt; abb<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string breakPalindrome(string palindrome) {\n    const int n = palindrome.length();\n    if (n == 1) return \"\";\n    for (int i = 0; i < n \/ 2; ++i) {\n      if (palindrome[i] != 'a') {\n        palindrome[i] = 'a';\n        return palindrome;\n      }\n    }\n    palindrome.back() = 'b';\n    return palindrome;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a palindromic string&nbsp;palindrome, replace&nbsp;exactly one&nbsp;character by any lowercase English letter so that the string becomes the lexicographically smallest possible string that&nbsp;isn&#8217;t&nbsp;a palindrome. After doing&#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],"tags":[88,177,376,95,4],"class_list":["post-6127","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-medium","tag-on","tag-palindrome","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6127","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=6127"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6127\/revisions"}],"predecessor-version":[{"id":6129,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6127\/revisions\/6129"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}