给定长度为N的小写字母组成的字符串S ,任务是找到使字符串S的所有字符相同所需的最少操作数。在每个操作中,选择任何字符并将其替换为下一个或上一个字母。
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.
例子:
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.
方法:解决该问题的方法是,计算使所有字符等于每个字母“ a”至“ z”的成本,并打印出任何转换所需的最低成本。请按照以下步骤解决问题:
- 用较大的值初始化变量min ,该值将存储最小答案。
- 遍历范围[ 0,25 ] ,其中i表示从‘a’到‘z’的第(i + 1)个字母,并执行以下步骤:
- 初始化变量cnt初始化为0 ,它将存储答案以转换相同字符串的所有字符。
- 将给定的字符串从j = 0遍历到(N – 1)并将min(abs(i +’a’– S [j]),26 – abs(i +’ a’– S [j]))加到cnt中。
- 完成上述步骤后,将min更新为min和cnt的最小值。
- 遍历每个字符的字符串后,输出min的值作为最小操作数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum count
// of operations to make all characters
// of the string same
int minCost(string s, int n)
{
// Set min to some large value
int minValue = 100000000;
// Find minimum operations for
// each character
for (int i = 0; i <= 25; i++) {
// Initialize cnt
int cnt = 0;
for (int j = 0; j < n; j++) {
// Add the value to cnt
cnt += min(abs(i - (s[j] - 'a')),
26 - abs(i - (s[j] - 'a')));
}
// Update minValue
minValue = min(minValue, cnt);
}
// Return minValue
return minValue;
}
// Driver Code
int main()
{
// Given string str
string str = "geeksforgeeks";
int N = str.length();
// Function Call
cout << minCost(str, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the minimum count
// of operations to make all characters
// of the String same
static int minCost(String s, int n)
{
// Set min to some large value
int minValue = 100000000;
// Find minimum operations for
// each character
for(int i = 0; i <= 25; i++)
{
// Initialize cnt
int cnt = 0;
for(int j = 0; j < n; j++)
{
// Add the value to cnt
cnt += Math.min(Math.abs(i - (s.charAt(j) - 'a')),
26 - Math.abs(i - (s.charAt(j) - 'a')));
}
// Update minValue
minValue = Math.min(minValue, cnt);
}
// Return minValue
return minValue;
}
// Driver Code
public static void main (String[] args)
{
// Given String str
String str = "geeksforgeeks";
int N = str.length();
// Function call
System.out.println(minCost(str, N));
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the
# above approach
# Function to find the minimum
# count of operations to make
# all characters of the string same
def minCost(s, n):
# Set min to some
# large value
minValue = 100000000
# Find minimum operations
# for each character
for i in range(26):
# Initialize cnt
cnt = 0
for j in range(n):
# Add the value to cnt
cnt += min(abs(i - (ord(s[j]) -
ord('a'))),
26 - abs(i - (ord(s[j]) -
ord('a'))))
# Update minValue
minValue = min(minValue, cnt)
# Return minValue
return minValue
# Driver Code
if __name__ == "__main__":
# Given string str
st = "geeksforgeeks"
N = len(st)
# Function Call
print(minCost(st, N))
# This code is contributed by Chitranayal
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum count
// of operations to make all characters
// of the String same
static int minCost(string s, int n)
{
// Set min to some large value
int minValue = 100000000;
// Find minimum operations for
// each character
for(int i = 0; i <= 25; i++)
{
// Initialize cnt
int cnt = 0;
for(int j = 0; j < n; j++)
{
// Add the value to cnt
cnt += Math.Min(Math.Abs(i - (s[j] - 'a')),
26 - Math.Abs(i - (s[j] - 'a')));
}
// Update minValue
minValue = Math.Min(minValue, cnt);
}
// Return minValue
return minValue;
}
// Driver code
public static void Main()
{
// Given String str
string str = "geeksforgeeks";
int N = str.Length;
// Function call
Console.WriteLine(minCost(str, N));
}
}
// This code is contributed by code_hunt
60
时间复杂度: O(N * 26)
辅助空间: O(N)