Ctrl K

Palindrome After a Few Fixes

Medium
You are given a string text of lowercase English letters and a list of queries queries, where queries[i] = [left, right, k]. For each query, take the piece text[left..right] (inclusive). You may rearrange its letters in any order and then replace at most k of them with any lowercase English letters. The answer to the query is True if this can produce a palindrome, and False otherwise. Queries are independent: the original string never changes. Each replacement changes exactly one letter. Return the answers in the order of the queries.
Example 1
text="racecar", queries=[[0, 6, 0], [1, 3, 0], [1, 3, 1], [2, 5, 1]] → [True, False, True, True]

"racecar" is already a palindrome. "ace" cannot be rearranged into one without replacing anything, but one replacement is enough ("aca", say). "ceca" rearranges into "caac" with no replacement at all.

Example 2
text="xyzw", queries=[[0, 3, 1], [0, 3, 2], [2, 2, 0]] → [False, True, True]

"xyzw" has four different letters: one replacement is not enough, two are ("xyyx"). A single letter "z" is a palindrome.

Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • 1 ≤ len(queries) ≤ 10⁵
  • 0 ≤ left ≤ right < len(text)
  • 0 ≤ k ≤ len(text)
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEven Vowel Stretch MediumSplit Into Equal-XOR Halves MediumAlmost Even Substrings MediumThree-Letter Palindromes in a Word MediumOne New Letter Medium
loading Python…
Input
text="racecar", queries=[[0, 6, 0], [1, 3, 0], [1, 3, 1], [2, 5, 1]]
Expected
[True, False, True, True]

Run checks the first 3 tests in your browser. Submit checks all 95 tests on the server, hidden ones included.