KatInterview

Check Palindrome Transformation Within K Removals

Dynamic ProgrammingAuto-graded · 29 test cases
MediumAuto-gradedDynamic Programmingdynamic-programmingstringsbit-manipulation

Last 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) <= 2000
  • s consists 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.