Minimum cost to convert string into palindrome | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cost ( string str ) { int len = str . length ( ) ; int res = 0 ; for ( int i = 0 , j = len - 1 ; i < j ; i ++ , j -- ) if ( str [ i ] != str [ j ] ) res += min ( str [ i ] , str [ j ] ) - ' a ' + 1 ; return res ; } int main ( ) { string str = " abcdef " ; cout << cost ( str ) << endl ; return 0 ; }