{"id":323,"date":"2017-09-17T22:27:33","date_gmt":"2017-09-18T05:27:33","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=323"},"modified":"2018-07-10T18:44:36","modified_gmt":"2018-07-11T01:44:36","slug":"leetcode-680-valid-palindrome-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-680-valid-palindrome-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 680. Valid Palindrome II"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 680. Valid Palindrome II - \u5237\u9898\u627e\u5de5\u4f5c EP60\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/hvI-rJyG4ik?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given a non-empty string\u00a0<code>s<\/code>, you may delete\u00a0<b>at most<\/b>\u00a0one character. Judge whether you can make it a palindrome.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: \"aba\"\r\nOutput: True\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: \"abca\"\r\nOutput: True\r\nExplanation: You could delete the character 'c'.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The string will only contain lowercase characters a-z. The maximum length of the string is 50000.<\/li>\n<\/ol>\n<p><strong>Idea:<\/strong><\/p>\n<p>Greedy, delete the first unmatched char<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/680-ep60.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-326\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/680-ep60.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/680-ep60.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/680-ep60-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/680-ep60-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/680-ep60-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Time complexity<\/strong><\/p>\n<p>O(n)<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 118 ms\r\nclass Solution {\r\npublic:\r\n    bool validPalindrome(const string&amp; s) {\r\n        int l = -1;\r\n        int r = s.length();\r\n        while (++l &lt; --r)\r\n            if (s[l] != s[r])\r\n                return isPalindrome(s, l+1, r) \r\n                    || isPalindrome(s, l, r-1);\r\n        return true;\r\n    }\r\nprivate:\r\n    bool isPalindrome(const string&amp; s, int l, int r) {\r\n        while (l &lt; r)\r\n            if (s[l++] != s[r--]) return false;\r\n        return true;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given a non-empty string\u00a0s, you may delete\u00a0at most\u00a0one character. Judge whether you can make it a palindrome. Example 1: Input: &#8220;aba&#8221; Output: True Example&#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":[222,95,4],"class_list":["post-323","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-palindrome","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/323","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=323"}],"version-history":[{"count":10,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/323\/revisions"}],"predecessor-version":[{"id":3070,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/323\/revisions\/3070"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}