<p>You are given a string <code>s</code> and array <code>queries</code> where <code>queries[i] = [left<sub>i</sub>, right<sub>i</sub>, k<sub>i</sub>]</code>. We may rearrange the substring <code>s[left<sub>i</sub>...right<sub>i</sub>]</code> for each query and then choose up to <code>k<sub>i</sub></code> of them to replace with any lowercase English letter.</p>

<p>If the substring is possible to be a palindrome string after the operations above, the result of the query is <code>true</code>. Otherwise, the result is <code>false</code>.</p>

<p>Return a boolean array <code>answer</code> where <code>answer[i]</code> is the result of the <code>i<sup>th</sup></code> query <code>queries[i]</code>.</p>

<p>Note that each letter is counted individually for replacement, so if, for example <code>s[left<sub>i</sub>...right<sub>i</sub>] = &quot;aaa&quot;</code>, and <code>k<sub>i</sub> = 2</code>, we can only replace two of the letters. Also, note that no query modifies the initial string <code>s</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example :</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abcda&quot;, queries = [[3,3,0],[1,2,0],[0,3,1],[0,3,2],[0,4,1]]
<strong>Output:</strong> [true,false,false,true,true]
<strong>Explanation:</strong>
queries[0]: substring = &quot;d&quot;, is palidrome.
queries[1]: substring = &quot;bc&quot;, is not palidrome.
queries[2]: substring = &quot;abcd&quot;, is not palidrome after replacing only 1 character.
queries[3]: substring = &quot;abcd&quot;, could be changed to &quot;abba&quot; which is palidrome. Also this can be changed to &quot;baab&quot; first rearrange it &quot;bacd&quot; then replace &quot;cd&quot; with &quot;ab&quot;.
queries[4]: substring = &quot;abcda&quot;, could be changed to &quot;abcba&quot; which is palidrome.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;lyb&quot;, queries = [[0,1,0],[2,2,1]]
<strong>Output:</strong> [false,true]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length, queries.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= left<sub>i</sub> &lt;= right<sub>i</sub> &lt; s.length</code></li>
	<li><code>0 &lt;= k<sub>i</sub> &lt;= s.length</code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>

class Solution {
private:
    const size_t ENG_LETTERS_COUNT = 26;
    std::vector<std::vector<int>> count;

public:
    void get_characters_count(const std::string &s, std::vector<std::vector<int>> &res) {
        for (size_t i = 1; i <= s.length(); i++) {
            count[i][s[i - 1] - 'a'] = 1; 
            for (size_t j = 0; j < ENG_LETTERS_COUNT; j++) {
                count[i][j] = count[i][j] + count[i - 1][j];
            }
        } 
    }

    void get_diff_characters_count(int low, int high, std::vector<int> &res) {
        for (size_t j = 0; j < ENG_LETTERS_COUNT; j++) {
            res[j] = count[high + 1][j] - count[low][j]; 
        }
    }

    bool can_make_palindrome(const std::string &str, int low, int high, int allowed_replacement_count) {
        std::vector<int> diff_count(ENG_LETTERS_COUNT);
        get_diff_characters_count(low, high, diff_count);

        int need_replacement_characters_count = (high - low + 1) / 2;
        for (size_t i = 0; i < ENG_LETTERS_COUNT; i++) {
            if (diff_count[i] > 0) {
                need_replacement_characters_count -= diff_count[i] / 2;
            }
        }

        return need_replacement_characters_count <= allowed_replacement_count;
    }

    vector<bool> canMakePaliQueries(string s, vector<vector<int>>& queries) {
        count = std::vector<std::vector<int>>(s.length() + 1, std::vector<int>(ENG_LETTERS_COUNT));
        get_characters_count(s, count);

        std::vector<bool> res(queries.size());
        for (size_t i = 0; i < queries.size(); i++) {
            int low = queries[i][0], high = queries[i][1], allowed_replacement_count = queries[i][2];
            res[i] = can_make_palindrome(s, low, high, allowed_replacement_count);
        }

        return res;
    }
};
176754