Minimum characters to be added at front to make string palindrome | function for checking string is palindrome or not ; Driver code ; if string becomes palindrome then break ; erase the last element of the string ; print the number of insertion at front
def ispalindrome ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE i = 0 NEW_LINE j = l - 1 NEW_LINE while i <= j : NEW_LINE INDENT if ( s [ i ] != s [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " BABABAA " NEW_LINE cnt = 0 NEW_LINE flag = 0 NEW_LINE while ( len ( s ) > 0 ) : NEW_LINE INDENT if ( ispalindrome ( s ) ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT cnt += 1 NEW_LINE s = s [ : - 1 ] NEW_LINE s . erase ( s . begin ( ) + s . length ( ) - 1 ) ; NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT print ( cnt ) NEW_LINE DEDENT DEDENT