# Minimum Insertion To Form A Palindrome in Java

A string is given to us. Our task is to convert that string into a palindromic string by inserting characters. The characters should be inserted only on the leftmost side of the input string. In the output, we have to mention the total number of characters that are being inserted in the input string to make it a palindrome string. See the following mentioned examples.

Example: 1

Input:

String str = "ab"

Output: 1

Explanation: If we insert the character 'b' in the leftmost side of the string, we get the string, "bab", which is a palindrome. Thus, the total number of inserted characters in the input string is 1.

Example: 2

Input:

String str = "abcd"

Output: 3

Explanation: If we insert the character 'b' in the leftmost side of the string we get the string, "dcbabcd", which is a palindrome. Thus, the total number of inserted characters in the input string is 3.

Example: 3

Input:

String str = "bab"

Output: 0

Explanation: The input string is already a palindrome string. Hence, no insertion of characters is required. Hence, the output is 0.

## Naïve Approach

In this approach, we will be using recursion. We will be using a method computeMinInsertions() that computes the minimum count of characters for making a palindromic string. Observe the following implementation.

FileName: MinInsertionPalindrome.java

Output:

```For the string: ab
The minimum number of characters that should be inserted to make it palindromic is: 1

For the string: abcd
The minimum number of characters that should be inserted to make it palindromic is: 3

For the string: bab
The minimum number of characters that should be inserted to make it palindromic is: 0
```

Complexity Analysis: The time complexity of the program is O(nn), and the space complexity of the program is O(n), where n is the total number of characters present in the input string.

The time complexity of the above program is very high and is not suitable for large inputs. The reason for the high time complexity is due to many sub-problems getting computed again and again. We can avoid the repeated computation of the sub-problems with the help of dynamic programming. The following approach shows the same.

## Approach: Using Dynamic Programming

We will be using a 2-Dimensional array to store the solution of the sub-problems so that they are not computed again and again. Also, we will use the stored sub-problem solution to compute the final solution.

FileName: MinInsertionPalindrome1.java

Output:

```For the string: ab
The minimum number of characters that should be inserted to make it palindromic is: 1

For the string: abcd
The minimum number of characters that should be inserted to make it palindromic is: 3

For the string: bab
The minimum number of characters that should be inserted to make it palindromic is: 0
```

Complexity Analysis: Since two loops are used (nested loops) in the program, the total time complexity of the program is O(n2). Also, a 2-dimensional array is used in the method computeMinInsertions(), making the space complexity of the program O(n2), where n is the total number of characters present in the input string.

## Another Approach: Using Longest Common Subsequence

Another approach is to use the concept of the LCS (Longest Common Subsequence) problem. Using this approach, we will find the LCS of the input string and the reverse of the input string.

FileName: MinInsertionPalindrome2.java

Output:

```For the string: ab
The minimum number of characters that should be inserted to make it palindromic is: 1

For the string: abcd
The minimum number of characters that should be inserted to make it palindromic is: 3

For the string: bab
The minimum number of characters that should be inserted to make it palindromic is: 0
```

Complexity Analysis: The time, as well as space complexity of the program is the same as the previous program.

We can still do optimization in terms of space complexity. Note that in the lcs table, we only need the current row and the previous row elements. The illustration of the same is given in the following program.

FileName: MinInsertionPalindrome3.java

Output:

```For the string: ab
The minimum number of characters that should be inserted to make it palindromic is: 1

For the string: abcd
The minimum number of characters that should be inserted to make it palindromic is: 3

For the string: bab
The minimum number of characters that should be inserted to make it palindromic is: 0
```

Complexity Analysis: The time complexity of the program is the same as the previous program. However, the space complexity of the program is better than the previous one, which is O(N), where N is the total number of characters present in the input string. It is because the program is only using single-dimensional arrays.

### Feedback   