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
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 += min ( abs ( i - ( s [ j ] - ' a ' ) ) , 26 - abs ( i - ( s [ j ] - ' a ' ) ) ) ; } minValue = min ( minValue , cnt ) ; } return minValue ; } int main ( ) { string str = " geeksforgeeks " ; int N = str . length ( ) ; cout << minCost ( str , N ) ; return 0 ; }