{"id":5516,"date":"2019-09-01T17:56:36","date_gmt":"2019-09-02T00:56:36","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5516"},"modified":"2019-09-01T17:57:04","modified_gmt":"2019-09-02T00:57:04","slug":"leetcode-1177-can-make-palindrome-from-substring","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1177-can-make-palindrome-from-substring\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1177. Can Make Palindrome from Substring"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>, we make queries on substrings of&nbsp;<code>s<\/code>.<\/p>\n\n\n\n<p>For each query&nbsp;<code>queries[i] = [left, right, k]<\/code>, we may&nbsp;<strong>rearrange<\/strong>&nbsp;the substring&nbsp;<code>s[left], ..., s[right]<\/code>, and then choose&nbsp;<strong>up to<\/strong>&nbsp;<code>k<\/code>&nbsp;of them to replace with any lowercase English letter.&nbsp;<\/p>\n\n\n\n<p>If the substring&nbsp;is possible to be a&nbsp;palindrome string after the operations above, the result of the query is&nbsp;<code>true<\/code>.&nbsp;Otherwise, the result&nbsp;is&nbsp;<code>false<\/code>.<\/p>\n\n\n\n<p>Return an array&nbsp;<code>answer[]<\/code>, where&nbsp;<code>answer[i]<\/code>&nbsp;is the result of the&nbsp;<code>i<\/code>-th query&nbsp;<code>queries[i]<\/code>.<\/p>\n\n\n\n<p>Note that: Each letter is counted&nbsp;<strong>individually<\/strong>&nbsp;for replacement so&nbsp;if for example&nbsp;<code>s[left..right] = \"aaa\"<\/code>, and&nbsp;<code>k = 2<\/code>, we can only replace two of the letters.&nbsp; (Also, note that the initial string&nbsp;<code>s<\/code>&nbsp;is never modified by any query.)<\/p>\n\n\n\n<p><strong>Example :<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"abcda\", queries = [[3,3,0],[1,2,0],[0,3,1],[0,3,2],[0,4,1]]\n<strong>Output:<\/strong> [true,false,false,true,true]\n<strong>Explanation:<\/strong>\nqueries[0] : substring = \"d\", is palidrome.\nqueries[1] :&nbsp;substring = \"bc\", is not palidrome.\nqueries[2] :&nbsp;substring = \"abcd\", is not palidrome after replacing only 1 character.\nqueries[3] :&nbsp;substring = \"abcd\", could be changed to \"abba\" which is palidrome. Also this can be changed to \"baab\" first rearrange it \"bacd\" then replace \"cd\" with \"ab\".\nqueries[4] :&nbsp;substring = \"abcda\",&nbsp;could be changed to \"abcba\" which is palidrome.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length,&nbsp;queries.length&nbsp;&lt;= 10^5<\/code><\/li><li><code>0 &lt;= queries[i][0] &lt;= queries[i][1] &lt;&nbsp;s.length<\/code><\/li><li><code>0 &lt;= queries[i][2] &lt;= s.length<\/code><\/li><li><code>s<\/code>&nbsp;only contains lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Prefix frequency<\/strong><\/h2>\n\n\n\n<p>Compute the prefix frequency of each characters, then we can efficiently compute the frequency of each characters in the substring in O(1) time. Count the number odd frequency characters o, we can convert it to a palindrome if o \/ 2 &lt;= k.<\/p>\n\n\n\n<p>Time complexity: <br>preprocessing: O(n)<br>Query: O(1)<br>Space complexity: O(n)<\/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  vector<bool> canMakePaliQueries(string s, vector<vector<int>>& queries) {\n    vector<bool> ans;\n    int n = s.length();\n    vector<vector<int>> f(n + 1, vector<int>(26));\n    for (int i = 0; i < n; ++i) {\n      ++f[i][s[i] - 'a'];\n      f[i + 1] = f[i];\n    }\n    for (const auto&#038; q : queries) {      \n      int l = q[0];\n      int r = q[1];\n      int k = q[2];      \n      int o = 0;\n      for (int i = 0; i < 26; ++i) {\n        int c = f[r][i] - (l == 0 ? 0 : f[l - 1][i]);        \n        o += c % 2;\n      }\n      ans.push_back(o \/ 2 <= k);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s, we make queries on substrings of&nbsp;s. For each query&nbsp;queries[i] = [left, right, k], we may&nbsp;rearrange&nbsp;the substring&nbsp;s[left], &#8230;, s[right], and then choose&nbsp;up to&nbsp;k&nbsp;of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[24,82,177,95,200],"class_list":["post-5516","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-frequency","tag-hashtable","tag-medium","tag-palindrome","tag-prefix-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5516","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=5516"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5516\/revisions"}],"predecessor-version":[{"id":5518,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5516\/revisions\/5518"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}