#1177

Can Make Palindrome from Substring

expert · 1020 · lc medium +32 · verified · 41.4% accepted · 876 likes · top 22%

Description

You are given a string s and an array queries where each queries[i] = [lefti, righti, ki]. For each query, consider the substring s[lefti...righti], which you may freely rearrange, then replace up to ki characters with any lowercase English letter.

Determine whether the substring can be made into a palindrome after these operations, storing true or false in answer[i].

Return the boolean array answer. Note: replacements are counted individually, and no query modifies the original string s.

Example 1:

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]

Code

1
2
3