给定长度为N的字符串S ,该字符串由小写字母和整数K组成,其中N%K = 0 ,任务是通过执行以下操作来找到将给定字符串转换为相同的K长度子字符串的串联字符串的最小开销操作:
- 一个字符可以替换为另一个字符。
- 每次操作的成本是被替换字符与替换字符之间的绝对差额。例如,如果将“ a”替换为“ z” ,则操作成本为|“ a”-“ z” |。 = 25 。
例子:
Input: S = “abcdef”, K = 2
Output: 8
Explanation:
One possible answer is “cdcdcd” and the repeatingK length substring is “cd”. The minimum cost to required to convert the string is calcualted by the following steps:
Step 1: Replace S[0] with “c”. Therefore, cost = |”a”-“c”| = 2.
Step 2: Replace S[1] with “d”. Therefore, cost = |”b”-“d”| = 2.
Step 3: Replace S[2] with “c”. Therefore, cost = |”c”-“c”| = 0.
Step 4: Replace S[3] with “d”. Therefore, cost = |”d”-“d”| = 0.
Step 5: Replace S[4] with “c”. Therefore, cost = |”e”-“c”| = 2.
Step 6: Replace S[5] with “d”. Therefore, cost = |”f”-“d”| = 2.
Therefore, the minimum cost required = 2 + 2 + 0 + 0 + 2 + 2 = 8.
Input: S = “abcabc”, K = 3
Output: 0
Explanation:
The given string already consists a repeating substring “abc” of length K
天真的方法:最简单的方法是生成所有可能的长度为K的排列,并找到转换给定字符串的成本,使其具有长度为K的重复模式。然后,打印其中的最低成本。
时间复杂度: O(N * K 26 ),其中N是给定字符串的长度,而K是给定整数。
辅助空间: O(N)
高效方法:想法是使用贪婪技术,并观察到对于从0到K – 1的任何位置i ,位置i + j * K处的字符必须相同,其中0≤j
- 初始化变量ans以存储所需的最低成本。
- 在[0,K – 1]范围内遍历字符串。
- 对于每个位置i ,找到将每个字符放置在位置i + j * K上的成本,对于每个字符,其中0≤j
计算其中的最小成本并更新ans 。 - 完成上述步骤后,将ans的值打印为所需的最低成本。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum cost
// to convert given String into
// String of K length same subString
void minCost(string s, int k)
{
// Stores length of String
int n = s.size();
// Stores the minimum cost
int ans = 0;
// Traverse left subString
// of k length
for(int i = 0; i < k; i++)
{
// Stores the frequency
int a[26];
for(int p = 0; p < 26; p++)
{
a[p] = 0;
}
for(int j = i; j < n; j += k)
{
a[s[j] - 'a']++;
}
// Stores minimum cost for
// sequence of S[i]%k indices
int min_cost = INT_MAX;
// Check for optimal charcter
for(int ch = 0; ch < 26; ch++)
{
int cost = 0;
// Find sum of distance 'a'+ ch
// from charcter S[i]%k indices
for(int tr = 0; tr < 26; tr++)
cost += abs(ch - tr) * a[tr];
// Choose minimum cost for
// each index i
min_cost = min(min_cost, cost);
}
// Increment ans
ans += min_cost;
}
// Print minimum cost to
// convert String
cout << (ans);
}
// Driver Code
int main()
{
// Given String S
string S = "abcdefabc";
int K = 3;
// Function Call
minCost(S, K);
}
// This code is contributed by gauravrajput1
Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
class GFG {
// Function to find minimum cost
// to convert given string into
// string of K length same substring
static void minCost(String s, int k)
{
// Stores length of string
int n = s.length();
// Stores the minimum cost
int ans = 0;
// Traverse left substring
// of k length
for (int i = 0; i < k; i++) {
// Stores the frequency
int[] a = new int[26];
for (int j = i; j < n; j += k) {
a[s.charAt(j) - 'a']++;
}
// Stores minimum cost for
// sequence of S[i]%k indices
int min_cost
= Integer.MAX_VALUE;
// Check for optimal charcter
for (int ch = 0; ch < 26; ch++) {
int cost = 0;
// Find sum of distance 'a'+ ch
// from charcter S[i]%k indices
for (int tr = 0; tr < 26; tr++)
cost += Math.abs(ch - tr)
* a[tr];
// Choose minimum cost for
// each index i
min_cost = Math.min(min_cost,
cost);
}
// Increment ans
ans += min_cost;
}
// Print minimum cost to
// convert string
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
// Given string S
String S = "abcdefabc";
int K = 3;
// Function Call
minCost(S, K);
}
}
Python3
# Python3 program for the
# above approach
import sys
# Function to find minimum cost
# to convert given string into
# string of K length same substring
def minCost(s, k):
# Stores length of string
n = len(s)
# Stores the minimum cost
ans = 0
# Traverse left substring
# of k length
for i in range(k):
# Stores the frequency
a = [0] * 26
for j in range(i, n, k):
a[ord(s[j]) - ord('a')] += 1
# Stores minimum cost for
# sequence of S[i]%k indices
min_cost = sys.maxsize - 1
# Check for optimal charcter
for ch in range(26):
cost = 0
# Find sum of distance 'a'+ ch
# from charcter S[i]%k indices
for tr in range(26):
cost += abs(ch - tr) * a[tr]
# Choose minimum cost for
# each index i
min_cost = min(min_cost,
cost)
# Increment ans
ans += min_cost
# Print minimum cost to
# convert string
print(ans)
# Driver Code
# Given string S
S = "abcdefabc"
K = 3
# Function call
minCost(S, K)
# This code is contributed by code_hunt
C#
// C# program for the
// above approach
using System;
class GFG{
// Function to find minimum cost
// to convert given string into
// string of K length same substring
static void minCost(string s, int k)
{
// Stores length of string
int n = s.Length;
// Stores the minimum cost
int ans = 0;
// Traverse left substring
// of k length
for(int i = 0; i < k; i++)
{
// Stores the frequency
int[] a = new int[26];
for(int j = i; j < n; j += k)
{
a[s[j] - 'a']++;
}
// Stores minimum cost for
// sequence of S[i]%k indices
int min_cost = Int32.MaxValue;
// Check for optimal charcter
for(int ch = 0; ch < 26; ch++)
{
int cost = 0;
// Find sum of distance 'a'+ ch
// from charcter S[i]%k indices
for(int tr = 0; tr < 26; tr++)
cost += Math.Abs(ch - tr) * a[tr];
// Choose minimum cost for
// each index i
min_cost = Math.Min(min_cost,
cost);
}
// Increment ans
ans += min_cost;
}
// Print minimum cost to
// convert string
Console.WriteLine(ans);
}
// Driver Code
public static void Main()
{
// Given string S
string S = "abcdefabc";
int K = 3;
// Function call
minCost(S, K);
}
}
// This code is contributed by sanjoy_62
9
时间复杂度: O(N + K)
辅助空间: O(N)