1177. Can Make Palindrome from Substring LeetCode Solution
In this guide, you will get 1177. Can Make Palindrome from Substring LeetCode Solution with the best time and space complexity. The solution to Can Make Palindrome from Substring problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Can Make Palindrome from Substring solution in C++
- Can Make Palindrome from Substring solution in Java
- Can Make Palindrome from Substring solution in Python
- Additional Resources

Problem Statement of 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]
Constraints:
1 <= s.length, queries.length <= 105
0 <= lefti <= righti < s.length
0 <= ki <= s.length
s consists of lowercase English letters.
Example not found
Constraints:
1 <= s.length, queries.length <= 105
0 <= lefti <= righti < s.length
0 <= ki <= s.length
s consists of lowercase English letters.
Complexity Analysis
- Time Complexity:
- Space Complexity:
1177. Can Make Palindrome from Substring LeetCode Solution in C++
class Solution {
public:
vector<bool> canMakePaliQueries(string s, vector<vector<int>>& queries) {
vector<bool> ans;
vector<unsigned int> dp(s.length() + 1);
for (int i = 1; i <= s.length(); ++i)
dp[i] = dp[i - 1] ^ 1 << s[i - 1] - 'a';
for (const vector<int>& query : queries) {
const int odds = popcount(dp[query[1] + 1] ^ dp[query[0]]);
ans.push_back(odds / 2 <= query[2]);
}
return ans;
}
};
/* code provided by PROGIEZ */
1177. Can Make Palindrome from Substring LeetCode Solution in Java
class Solution {
public List<Boolean> canMakePaliQueries(String s, int[][] queries) {
List<Boolean> ans = new ArrayList<>();
int[] dp = new int[s.length() + 1];
for (int i = 1; i <= s.length(); ++i)
dp[i] = dp[i - 1] ^ 1 << s.charAt(i - 1) - 'a';
for (int[] query : queries) {
int odds = Integer.bitCount(dp[query[1] + 1] ^ dp[query[0]]);
ans.add(odds / 2 <= query[2]);
}
return ans;
}
}
// code provided by PROGIEZ
1177. Can Make Palindrome from Substring LeetCode Solution in Python
class Solution:
def canMakePaliQueries(self, s: str, queries: list[list[int]]) -> list[bool]:
dp = [0] * (len(s) + 1)
for i in range(1, len(s) + 1):
dp[i] = dp[i - 1] ^ 1 << ord(s[i - 1]) - ord('a')
return [
(dp[right + 1] ^ dp[left]).bit_count() // 2 <= k
for left, right, k in queries
]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.