Minimum characters to be added at front to make string palindrome | Java program for getting minimum character 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
class GFG { static boolean ispalindrome ( String s ) { int l = s . length ( ) ; for ( int i = 0 , j = l - 1 ; i <= j ; i ++ , j -- ) { if ( s . charAt ( i ) != s . charAt ( j ) ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String s = " BABABAA " ; int cnt = 0 ; int flag = 0 ; while ( s . length ( ) > 0 ) { if ( ispalindrome ( s ) ) { flag = 1 ; break ; } else { cnt ++ ; s = s . substring ( 0 , s . length ( ) - 1 ) ; s . erase ( s . begin ( ) + s . length ( ) - 1 ) ; } } if ( flag == 1 ) { System . out . println ( cnt ) ; } } }