Minimize replacement of characters to its nearest alphabet to make a string palindromic | Java program for the above approach ; Function to find the minimum number of operations required to convert th given string into palindrome ; Iterate till half of the string ; Find the absolute difference between the characters ; Adding the minimum difference of the two result ; Return the result ; Driver code ; Given string ; Function call
class GFG { public static int minOperations ( String s ) { int len = s . length ( ) ; int result = 0 ; for ( int i = 0 ; i < len / 2 ; i ++ ) { int D1 = Math . max ( s . charAt ( i ) , s . charAt ( len - 1 - i ) ) - Math . min ( s . charAt ( i ) , s . charAt ( len - 1 - i ) ) ; int D2 = 26 - D1 ; result += Math . min ( D1 , D2 ) ; } return result ; } public static void main ( String [ ] args ) { String s = " abccdb " ; System . out . println ( minOperations ( s ) ) ; } }