Minimum cost to convert string into palindrome | Java program to find minimum cost to make a palindrome . ; Function to return cost ; length of string ; Iterate from both sides of string . If not equal , a cost will be there ; Driver code
import java . io . * ; class GFG { static int cost ( String str ) { int len = str . length ( ) ; int res = 0 ; for ( int i = 0 , j = len - 1 ; i < j ; i ++ , j -- ) if ( str . charAt ( i ) != str . charAt ( j ) ) res += Math . min ( str . charAt ( i ) , str . charAt ( j ) ) - ' a ' + 1 ; return res ; } public static void main ( String [ ] args ) { String str = " abcdef " ; System . out . println ( cost ( str ) ) ; } }