Minimize replacements by previous or next alphabet required to make all characters of a string the same | C # 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
using System ; 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 [ j ] - ' a ' ) ) , 26 - Math . Abs ( i - ( s [ j ] - ' a ' ) ) ) ; } minValue = Math . Min ( minValue , cnt ) ; } return minValue ; } public static void Main ( ) { string str = " geeksforgeeks " ; int N = str . Length ; Console . WriteLine ( minCost ( str , N ) ) ; } }