Minimize replacements by previous or next alphabet required to make all characters of a string the same | Java program for the above approach ; Function to find the minimum count of operations to make all characters of the String same ; Set min to some large value ; Find minimum operations for each character ; Initialize cnt ; Add the value to cnt ; Update minValue ; Return minValue ; Driver Code ; Given String str ; Function call
import java . io . * ; class GFG { static int minCost ( String s , int n ) { int minValue = 100000000 ; for ( int i = 0 ; i <= 25 ; i ++ ) { int cnt = 0 ; for ( int j = 0 ; j < n ; j ++ ) { cnt += Math . min ( Math . abs ( i - ( s . charAt ( j ) - ' a ' ) ) , 26 - Math . abs ( i - ( s . charAt ( j ) - ' a ' ) ) ) ; } minValue = Math . min ( minValue , cnt ) ; } return minValue ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int N = str . length ( ) ; System . out . println ( minCost ( str , N ) ) ; } }