Given string str, the task is to find the minimum number of characters to be inserted to convert it to a palindrome.
Cpp Program Code:
Here, we are using dynamic programming to solve the problem. We define a 2D array dp of size n x n, where n is the length of the input string str. dp[i][j] represents the minimum number of insertions required to convert the substring str[i..j] to a palindrome. We initialize dp[i][j] to 0 for all i = j, since a single character is already a palindrome.
Python Program code:
Here, we are using dynamic programming to solve the problem. We define a 2D list dp of size n x n, where n is the length of the input string str. dp[i][j] represents the minimum number of insertions required to convert the substring str[i..j] to a palindrome. We initialize dp[i][j] to 0 for all i = j, since a single character is already a palindrome.