Minimum insertions to form a palindrome with permutations allowed | Python3 program to find minimum number of insertions to make a string palindrome ; Function will return number of characters to be added ; To store str1ing length ; To store number of characters occurring odd number of times ; To store count of each character ; To store occurrence of each character ; To count characters with odd occurrence ; As one character can be odd return res - 1 but if str1ing is already palindrome return 0 ; Driver Code
import math as mt NEW_LINE def minInsertion ( tr1 ) : NEW_LINE INDENT n = len ( str1 ) NEW_LINE res = 0 NEW_LINE count = [ 0 for i in range ( 26 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT count [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( count [ i ] % 2 == 1 ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT if ( res == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return res - 1 NEW_LINE DEDENT DEDENT str1 = " geeksforgeeks " NEW_LINE print ( minInsertion ( str1 ) ) NEW_LINE