> For the complete documentation index, see [llms.txt](https://soumyajit4419.gitbook.io/ds-algo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://soumyajit4419.gitbook.io/ds-algo/strings/33.-can-make-palindrome-from-substring.md).

# 33. Can Make Palindrome from Substring

You are given a string `s` and array `queries` where `queries[i] = [lefti, righti, ki]`. We may rearrange the substring `s[lefti...righti]` for each query and then choose up to `ki` of them to replace with any lowercase English letter.

If the substring is possible to be a palindrome string after the operations above, the result of the query is `true`. Otherwise, the result is `false`.

Return a boolean array `answer` where `answer[i]` is the result of the `ith` query `queries[i]`.

Note that each letter is counted individually for replacement, so if, for example `s[lefti...righti] = "aaa"`, and `ki = 2`, we can only replace two of the letters. Also, note that no query modifies the initial string `s`.

**Example :**

```
Input: s = "abcda", queries = [[3,3,0],[1,2,0],[0,3,1],[0,3,2],[0,4,1]]
Output: [true,false,false,true,true]
Explanation:
queries[0]: substring = "d", is palidrome.
queries[1]: substring = "bc", is not palidrome.
queries[2]: substring = "abcd", is not palidrome after replacing only 1 character.
queries[3]: 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".
queries[4]: substring = "abcda", could be changed to "abcba" which is palidrome.
```

**Example 2:**

```
Input: s = "lyb", queries = [[0,1,0],[2,2,1]]
Output: [false,true]
```

## Solution: (Prefix Sum)

```cpp
class Solution
{
public:
    vector<bool> canMakePaliQueries(string s, vector<vector<int>> &queries)
    {

        vector<vector<int>> ps;
        vector<int> tmp(26, 0);
        
        ps.push_back(tmp);
        
        for (int i = 0; i < s.length(); i++)
        {
            tmp[s[i] - 97]++;
            ps.push_back(tmp);
        }

        vector<bool> ans;
        
        for (int i = 0; i < queries.size(); i++)
        {
            vector<int> end = ps[queries[i][1]+1];
            vector<int> start = ps[queries[i][0]];
            
            int oddNum = 0;

            for (int i = 0; i < 26; i++)
            {
                int count = end[i] - start[i];

                if (count % 2 != 0)
                {
                    oddNum += count % 2;
                }
         
            }

            if (oddNum % 2 != 0)
            {
                oddNum--;
            }
            

            if (oddNum <= 2 * queries[i][2])
            {
                ans.push_back(true);
            }
            else
            {
                ans.push_back(false);
            }
        }

        return ans;
    }
};
```
