Check Palindrome Transformation Within K Removals
Dynamic ProgrammingAuto-graded · 29 test casesLast updated
Determine if a given string can be converted into a palindrome by removing at most k characters.
Given a string s and a non-negative integer k, determine whether it is possible to transform s into a valid palindrome by removing at most k characters.
A string is a palindrome if it reads the same forward and backward. Deleting a character does not shift the index alignment of remaining characters relative to each other; it leaves a subsequence of the original string.
Constraints
0 <= len(s) <= 2000sconsists of printable ASCII characters or lower/upper case letters.k >= 0
Examples
Example 1
Input: can_make_palindrome_with_k_deletions('abcdeca', 2)
Output: True
Example 2
Input: can_make_palindrome_with_k_deletions('abbca', 1)
Output: True
Example 3
Input: can_make_palindrome_with_k_deletions('racecar', 0)
Output: True
Example 4
Input: can_make_palindrome_with_k_deletions('abcdef', 2)
Output: False
Hints
Hint 1
Removing the minimum number of characters to form a palindrome is equivalent to finding the Longest Palindromic Subsequence (LPS) of the string.
Hint 2
The length of the Longest Palindromic Subsequence of a string `s` is equal to the Longest Common Subsequence (LCS) of `s` and its reverse `s[::-1]`.
Hint 3
If `len(s) - LPS(s) <= k`, then the answer is true; otherwise false.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To determine if s can become a palindrome after removing at most k characters, we want to maximize the number of characters we keep. The kept characters form a palindromic subsequence of s.
-
Relation to Longest Palindromic Subsequence (LPS):
- The minimum number of character removals required to make
sa palindrome islen(s) - LPS(s). - Therefore, the condition to satisfy is
len(s) - LPS(s) <= k.
- The minimum number of character removals required to make
-
LPS via Longest Common Subsequence (LCS):
- A string's LPS is equal to the LCS between the string
sand its reversals[::-1].
- A string's LPS is equal to the LCS between the string
-
Bit-Parallel Dynamic Programming:
- While standard 2D dynamic programming computes LCS in $O(n^2)$ time and $O(n^2)$ or $O(n)$ space, we can accelerate the DP using bit-parallel operations (Hyyrö / Allison–Dix algorithm).
- In Python, arbitrary-precision integers allow representing entire rows of the DP table as bit vectors.
- For each character in
s[::-1], bitwise operations update the set of active alignment boundaries in $O(1)$ operations per character step (effectively $O(n^2 / w)$ work).
Complexity
- Time Complexity: $O(n^2 / w)$, where $n$ is the length of
sand $w$ is the machine word size (or bit length). For typical input bounds ($n \le 2000$), this executes almost instantaneously. - Space Complexity: $O(n)$ to store bitmasks corresponding to character positions and state.
def can_make_palindrome_with_k_deletions(s: str, k: int) -> bool:
n = len(s)
if n == 0:
return True
positions = {}
for i, ch in enumerate(s):
positions[ch] = positions.get(ch, 0) | (1 << i)
state = 0
for ch in reversed(s):
matches = positions.get(ch, 0)
x = matches | state
y = (state << 1) | 1
state = x & ~(x - y)
longest_palindromic_subsequence = state.bit_count()
minimum_deletions = n - longest_palindromic_subsequence
return minimum_deletions <= k