Algorithm
Problem Name: 1177. 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.
Code Examples
#1 Code Example with Java Programming
Code -
Java Programming
class Solution {
public List canMakePaliQueries(String s, int[][] queries) {
int[][] count = new int[s.length() + 1][26];
for (int i = 0; i < s.length(); i++) {
count[i + 1] = count[i].clone();
count[i + 1][s.charAt(i) - 'a']++;
}
List list = new ArrayList<>();
for (int[] query : queries) {
int sum = 0;
for (int i = 0; i < 26; i++) {
sum += (count[query[1] + 1][i] - count[query[0]][i]) % 2;
}
list.add(sum / 2 <= query[2]);
}
return list;
}
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
const canMakePaliQueries = function(s, queries) {
const code = ch => ch.charCodeAt(0) - 'a'.charCodeAt(0)
const preCount = [...s].reduce(
(a, c) => {
let nc = a[a.length - 1]
nc ^= 1 << code(c) //NOT on one bit
a.push(nc)
return a
},
[0]
)
return queries.map(q => {
let subCount = preCount[q[1] + 1] ^ preCount[q[0]]
let oddChs = 0
while (subCount > 0) {
oddChs += subCount & 1
subCount >>= 1
}
return Math.floor(oddChs / 2) <= q[2]
})
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Python Programming
Code -
Python Programming
class Solution:
def canMakePaliQueries(self, s: str, queries: List[List[int]]) -> List[bool]:
cnts = [{}]
for i, c in enumerate(s):
cnts.append(dict(cnts[-1]))
cnts[-1][c] = cnts[-1].get(c, 0) + 1
res = []
for i, j, k in queries:
res.append(sum((v - cnts[i].get(k, 0)) % 2 for k, v in cnts[j + 1].items()) - k * 2 <= 1)
return res
Copy The Code &
Try With Live Editor
Input
Output