DEV Community

Jaspreet singh
Jaspreet singh

Posted on

Minimum Insertions to Make String Palindrome

Intuition

At first glance, it seems like we need to decide where to insert characters to make the string a palindrome.

Instead of thinking about what to insert, think about what we can keep.

The characters that are already part of the Longest Palindromic Subsequence (LPS) do not need any insertion.

Only the remaining characters need to be inserted.

For example,

String

abcda
Enter fullscreen mode Exit fullscreen mode

The Longest Palindromic Subsequence is

aca
Enter fullscreen mode Exit fullscreen mode

Length = 3

Characters not in LPS

b
d
Enter fullscreen mode Exit fullscreen mode

These are the characters we need to insert.

Hence,

Minimum Insertions = Length of String - Length of LPS
Enter fullscreen mode Exit fullscreen mode

Now the problem becomes finding the Longest Palindromic Subsequence (LPS).

Even better,

The Longest Palindromic Subsequence is simply the Longest Common Subsequence (LCS) between the string and its reverse.

Example

Original

abcda

Reverse

adcba
Enter fullscreen mode Exit fullscreen mode

LCS

aca
Enter fullscreen mode Exit fullscreen mode

Length = 3

Answer

5 - 3 = 2
Enter fullscreen mode Exit fullscreen mode

Brute Force Approach

Try every possible insertion recursively.

At every mismatch,

  • Insert the left character on the right.
  • Insert the right character on the left.

Take the minimum of both possibilities.

Example

abc

↓

Insert 'a'

↓

Insert 'b'

↓

Insert 'c'
Enter fullscreen mode Exit fullscreen mode

Since every mismatch creates two recursive choices, the number of possibilities grows exponentially.

Complexity

  • Time: O(2^N)
  • Space: O(N) (Recursion Stack)

Optimal Approach (Dynamic Programming)

Observation

Minimum Insertions

=

Length of String

-

Longest Palindromic Subsequence
Enter fullscreen mode Exit fullscreen mode

And,

Longest Palindromic Subsequence

=

Longest Common Subsequence

(String, Reverse(String))
Enter fullscreen mode Exit fullscreen mode

Algorithm

  1. Reverse the string.
  2. Find the Longest Common Subsequence (LCS) between the original string and the reversed string.
  3. Return:
Length - LCS
Enter fullscreen mode Exit fullscreen mode

Example

String

mbadm

Reverse

mdabm
Enter fullscreen mode Exit fullscreen mode

LCS

mam
Enter fullscreen mode Exit fullscreen mode

Length

3
Enter fullscreen mode Exit fullscreen mode

Answer

5 - 3 = 2
Enter fullscreen mode Exit fullscreen mode

Java Code

public int minInsertions(String s) {

    String rev = new StringBuilder(s).reverse().toString();

    int n = s.length();
    int[][] dp = new int[n + 1][n + 1];

    for (int i = 1; i <= n; i++) {

        for (int j = 1; j <= n; j++) {

            if (s.charAt(i - 1) == rev.charAt(j - 1)) {
                dp[i][j] = 1 + dp[i - 1][j - 1];
            } else {
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }

    return n - dp[n][n];
}
Enter fullscreen mode Exit fullscreen mode

Complexity

  • Time: O(N²)
  • Space: O(N²)

Interview One-Liner: Convert the problem into finding the Longest Palindromic Subsequence, which can be computed as the Longest Common Subsequence between the string and its reverse.

Top comments (0)