{"id":11804,"date":"2024-09-17T00:13:16","date_gmt":"2024-09-17T00:13:16","guid":{"rendered":"https:\/\/www.softwareeverydayblog.com\/?p=11804"},"modified":"2024-09-17T21:45:36","modified_gmt":"2024-09-17T21:45:36","slug":"programming-problem-4","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=11804","title":{"rendered":"[Programming Problem] Valid Palindrome IV"},"content":{"rendered":"<p>Valid Palindrome IV<\/p>\n<p>[<a href=\"https:\/\/leetcode.com\/problems\/valid-palindrome-iv\/description\/\" rel=\"noopener\" target=\"_blank\">Problem Link<\/a>]<\/p>\n<pre lang=\"text\">\r\nYou are given a 0-indexed string s consisting of only lowercase English letters.\r\nIn one operation, you can change any character of s to any other character.\r\n\r\nReturn true if you can make s a palindrome after performing exactly one or two operations, or return false otherwise.\r\n<\/pre>\n<p>Simply use the two pointer approach to check if string is a palindrome<br \/>\n&#8211; Start comparing first and last character<br \/>\n&#8211; If there&#8217;s a mismatch (i.e. characters are different), add &#8216;1&#8217; to the mismatch count. What does this mean? It means changing any one of the characters to the other will cause the string to be a (potential) palindrome.<br \/>\n&#8211; At the end if `(mismatch_count is <= 2) ? true : false`\n\n\n\n<pre lang=\"javascript\">\r\n\/**\r\n * @param {string} s\r\n * @return {boolean}\r\n *\/\r\nvar makePalindrome = function(s) {\r\n    let i = 0;\r\n    let j = s.length &#8211; 1;\r\n    let mismatch = 0;\r\n\r\n    while ( i < j ) {\r\n        if (s[i++] === s[j--]) continue;\r\n        mismatch++;\r\n        if (mismatch > 2) return false;\r\n    }\r\n\r\n    return mismatch <= 2;\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Valid Palindrome IV [Problem Link] You are given a 0-indexed string s consisting of only lowercase English letters. In one operation, you can change any character of s to any other character. Return true if you can make s a palindrome after performing exactly one or two operations, or return false otherwise. Simply use the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11804","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11804"}],"version-history":[{"count":4,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11804\/revisions"}],"predecessor-version":[{"id":11808,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11804\/revisions\/11808"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}