5 Best Ways to Find Minimum Number of Characters to Be Added to Make It Palindrome in Python

Rate this post

πŸ’‘ Problem Formulation: If you’ve been challenged with finding the shortest number of additional characters required to transform any given string into a palindrome, you’re in the right place. Consider the input string “abc”. The desired output would be 2, since adding “bc” in reverse order would result in “abcba”, a valid palindrome.

Method 1: Dynamic Programming Approach

The dynamic programming approach involves building a table that stores the results of subproblems to find the minimum number of insertions required. Specifically, this method finds the length of the longest palindromic subsequence and calculates the difference from the original string length.

Here’s an example:

def min_insertions_to_palindrome(s):
    n = len(s)
    dp = [[0] * n for _ in range(n)]
    for length in range(2, n + 1):
        for start in range(n - length + 1):
            end = start + length - 1
            if s[start] == s[end]:
                dp[start][end] = dp[start + 1][end - 1]
                dp[start][end] = min(dp[start][end - 1], dp[start + 1][end]) + 1
    return dp[0][n - 1]


Output: 2

This function calculates the minimum insertions based on dynamic programming. It progressively builds up a solution by comparing individual characters and determining the minimum number of insertions needed at each step while storing these results to avoid redundant work.

Method 2: Recursive Approach

The recursive solution explores all possibilities of making a palindrome by recursively computing the minimum insertions needed at each step. This method is simple conceptually but can be inefficient for larger strings due to repeated calculations.

Here’s an example:

def find_min_insertions(s, l, h):
    if l > h:
        return float('inf')
    if l == h:
        return 0
    if l == h - 1:
        return 0 if s[l] == s[h] else 1
    return (find_min_insertions(s, l + 1, h - 1) if s[l] == s[h] else 
            min(find_min_insertions(s, l, h - 1), 
                find_min_insertions(s, l + 1, h)) + 1)

input_str = "abc"
print(find_min_insertions(input_str, 0, len(input_str)-1))

Output: 2

This recursive function checks each possibility of creating a palindrome from both ends of the string, combining the best outcomes to find the minimum insertions. However, it becomes less practical as the string length increases due to the exponential increase in computations.

Method 3: Using Longest Palindromic Subsequence

This method calculates the longest palindromic subsequence (LPS) in the given string and then uses its length to determine the minimum number of insertions required. The minimum number of insertions is the length of the given string minus the length of the LPS.

Here’s an example:

def longest_palindromic_subsequence(s):
    return s == s[::-1] and len(s) or max(

def min_insertions(s):
    lps = longest_palindromic_subsequence(s)
    return len(s) - lps


Output: 2

This snippet first defines a function to find the length of the longest palindromic subsequence. Then it defines another function to calculate the minimum insertions based on that length. The strength of this method lies in its simplicity, but similarly to the recursive approach, it poses performance issues with larger strings.

Method 4: Greedy Approach with Reversal Check

The greedy approach advocates checking for the matching prefix and suffix characters while incrementally building a reverse string. The number of add-ons required is related to the unmatched characters after traversing from both ends of the string.

Here’s an example:

def min_additions_for_palindrome(s):
    rev_s = s[::-1]
    for i in range(len(s)):
        if s[i:] == rev_s[:len(s)-i]:
            return i
    return len(s)


Output: 2

This function reverses the string and checks for the largest matching suffix and prefix. If a match is found, the index indicates the number of characters needed to make the string a palindrome. This method is efficient as it directly arrives at the solution with a single pass.

Bonus One-Liner Method 5: Pythonic Approach with itertools

Utilizing Python’s itertools module, we can create a one-liner that combines methods from the itertools and built-in functions to find the minimum additions. Although concise, this approach is not necessarily efficient and may not be suitable for very large strings.

Here’s an example:

from itertools import combinations

def min_insertions_palindrome(s):
    return len(s) - max(len(''.join(c)) for i in range(len(s)+1) for c in combinations(s, i) if ''.join(c) == ''.join(c)[::-1])


Output: 2

This function relies on generating all possible combinations of the characters in the string and checks for the longest palindromic combination. It then subtracts this length from the original string’s length to provide the minimum insertions required.


  • Method 1: Dynamic Programming Approach. It is computationally efficient for medium-sized strings and avoids redundant calculations. However, it may have increased memory overhead due to DP table storage.
  • Method 2: Recursive Approach. It is conceptually straightforward but may suffer performance issues with lengthy strings due to a lack of memoization.
  • Method 3: Longest Palindromic Subsequence. This method has intuitive appeal but shares the same drawbacks as the recursive approach, with steep time complexity for substantial strings.
  • Method 4: Greedy Approach with Reversal Check. It’s typically more efficient and runs with linear time complexity. Moreover, it handles larger sequences better than previous methods.
  • Method 5: Pythonic Approach with itertools. Offers a concise solution suitable for small strings, but not advised for large strings due to its potential to execute slower as it evaluates every combination.