5 Best Ways to Check If a Palindrome Can Be Formed After Deleting At Most K Characters in Python

πŸ’‘ Problem Formulation: The task is to determine if a given string can become a palindrome upon deleting at most ‘k’ characters. For instance, if the input string is “abecbea” and ‘k’ is 1, the desired output is ‘True’ because by removing the character ‘c’, the string becomes a palindrome “abebea”.

Method 1: Recursive Approach

The recursive approach checks palindrome validity from the string’s outer pairs moving towards the center. If a mismatch is found, it recursively checks two scenariosβ€”excluding either the left or right character. This method keeps track of the number of deletions and stops if it exceeds k.

Here’s an example:

def is_k_palindrome(s, k):
    def is_palindrome_range(i, j):
        if i >= j: return 0
        if s[i] != s[j]:
            return min(is_palindrome_range(i+1, j), is_palindrome_range(i, j-1)) + 1
        return is_palindrome_range(i+1, j-1)
    return is_palindrome_range(0, len(s) - 1) <= k

# Example usage
print(is_k_palindrome("abecbea", 1))

Output: True

This function initiates a helper that checks if the string can be reduced to a palindrome within “k” deletions, by comparing a character set starting from the outermost characters moving inwards and considering cases for deletion at each step of a mismatch.

Method 2: Dynamic Programming

Dynamic programming improves the recursive approach by storing intermediate results, which avoids repeating computations. The idea is to create a table that keeps track of the minimum deletions needed for every substring, and then check if this value is less than or equal to k for the whole string.

Here’s an example:

def is_k_palindrome_dp(s, k):
    n = len(s)
    dp = [[0 for _ in range(n)] for _ in range(n)]
    for length in range(2, n + 1):
        for i in range(n - length + 1):
            j = i + length - 1
            if s[i] == s[j]:
                dp[i][j] = dp[i + 1][j - 1]
                dp[i][j] = min(dp[i + 1][j], dp[i][j - 1]) + 1
    return dp[0][n - 1] <= k

# Example usage
print(is_k_palindrome_dp("abecbea", 1))

Output: True

The function generates a table storing the minimum deletions needed to convert every possible substring to a palindrome. Eventually, it checks the entry related to the full string, if this is less or equal to the given k, the answer is True.

Method 3: Two Pointer Approach

The two pointer approach checks for palindrome by comparing characters from both ends of the string. When a mismatch occurs, it skips the mismatched character and continues checking. Each skip counts as a deletion and is tracked to ensure it does not exceed k.

Here’s an example:

def is_k_palindrome_two_pointers(s, k):
    def check_palindrome(l, r, skipped):
        while l = k: return False
                return check_palindrome(l+1, r, skipped+1) or check_palindrome(l, r-1, skipped+1)
            l += 1; r -= 1
        return True

    return check_palindrome(0, len(s) - 1, 0)

# Example usage
print(is_k_palindrome_two_pointers("abecbea", 1))

Output: True

By using two pointers, one at the start and one at the end, this method checks whether the string can be made into a palindrome within k deletions by either skipping the left or the right character whenever they do not match.

Method 4: Greedy Approach with Early Stopping

The greedy approach is similar to the two pointer method but optimized with an early stopping criterion. If the number of deletions required is already greater than k before reaching the string’s center, it stops and returns False.

Here’s an example:

def is_k_palindrome_greedy(s, k):
    i, j = 0, len(s) - 1
    while i < j:
        if s[i] != s[j]:
            if k == 0: return False
            k -= 1
            if s[i+1] == s[j]:
                i += 1
                j -= 1
            i += 1; j -= 1
    return True

# Example usage
print(is_k_palindrome_greedy("abecbea", 1))

Output: True

This greedy algorithm takes a more straightforward approach by deciding which character to skip without recursive calls. If the number of allowable deletes is exceeded, it immediately stops, making it potentially more efficient.

Bonus One-Liner Method 5: Pythonic Approach with Slicing

A Pythonic approach combines slicing and built-in functions to solve the problem in a compact but potentially less efficient way. It checks all possible k-deletions in a brute-force manner using list comprehension and all function.

Here’s an example:

is_k_palindrome_pythonic = lambda s, k: any(s[i:j] == s[i:j][::-1] for i in range(len(s)) for j in range(i, len(s)+1) if len(s) - (j - i) <= k)

# Example usage
print(is_k_palindrome_pythonic("abecbea", 1))

Output: True

This solution performs a brute-force check and relies on Python’s powerful list comprehensions and slicing features. However, its efficiency is lower due to the lack of optimization present in previous methods.


  • Method 1: Recursive Approach. Exhaustive and comprehensive. Can be slow without memoization.
  • Method 2: Dynamic Programming. Stores intermediate results for efficiency. More complex and uses extra space.
  • Method 3: Two Pointer Approach. Memory-efficient and clear logic. Might perform unnecessary checks.
  • Method 4: Greedy Approach with Early Stopping. Fast and intuitive. May not work for all edge cases.
  • Bonus Method 5: Pythonic Approach with Slicing. Elegant one-liner. Least efficient due to the brute-force nature.