Minimize replacements by previous or next alphabet required to make all characters of a string the same
Given a string S of length N consisting of lowercase alphabets, the task is to find the minimum number of operations required to make all the characters of the string S the same. In each operation, choose any character and replace it with its next or previous alphabet.
Note: The alphabets are considered to be cyclic i.e., Next character of z is considered to be a and previous character of a is considered to be z.
Examples:
Input: S = “abc”
Output: 2
Explanation:
To minimize the number of operation change all characters of the strings to ‘b’.
Operation 1: Change a to b.
Operation 2: Change c to b.
Input: S = “zzza”
Output: 1
Explanation:
To minimize the number of operation change all characters of the strings to ‘z’.
Operation 1: Change a to z.
Approach: To solve the problem, the idea is to calculate the cost of making all the characters equal to each alphabets, ‘a’ to ‘z’, one by one and print the minimum cost required for any of the conversions. Follow the steps below to solve the problem:
- Initialize the variable min with a large value which will store the minimum answer.
- Traverse over the range [0, 25] where i represent (i + 1)thalphabet from ‘a’ to ‘z’ and perform the following steps:
- Initialize variable cnt initialized as 0 which will store the answer to convert all character of string same.
- Traverse the given string from j = 0 to (N – 1) and add min(abs(i + ‘a’ – S[j]), 26 – abs(i + ‘a’ – S[j])) into cnt.
- After the above step, update min to a minimum of min and cnt.
- After traversing the string for each character, print the value of min as the minimum count of operations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
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));
}
}
|
Python3
def minCost(s, n):
minValue = 100000000
for i in range ( 26 ):
cnt = 0
for j in range (n):
cnt + = min ( abs (i - ( ord (s[j]) -
ord ( 'a' ))),
26 - abs (i - ( ord (s[j]) -
ord ( 'a' ))))
minValue = min (minValue, cnt)
return minValue
if __name__ = = "__main__" :
st = "geeksforgeeks"
N = len (st)
print (minCost(st, N))
|
C#
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));
}
}
|
Javascript
<script>
function minCost(s, n) {
var minValue = 100000000;
for ( var i = 0; i <= 25; i++) {
var cnt = 0;
for ( var j = 0; j < n; j++) {
cnt += Math.min(
Math.abs(i - (s[j].charCodeAt(0) -
"a" .charCodeAt(0))),
26 - Math.abs(i - (s[j].charCodeAt(0) -
"a" .charCodeAt(0)))
);
}
minValue = Math.min(minValue, cnt);
}
return minValue;
}
var str = "geeksforgeeks" ;
var N = str.length;
document.write(minCost(str, N));
</script>
|
Time Complexity: O(N * 26)
Auxiliary Space: O(1)
Last Updated :
15 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...