Minimize cost to convert given string into concatenation of equal substrings of length K
Given a string S of length N consisting of lowercase letters and an integer K, where N % K = 0, the task is to find the minimum cost to convert the given string into a concatenated string of the same K-length substrings by performing the following operations:
- A character can be replaced with another character.
- The cost of each operation is the absolute difference between the replaced and the replaced character. For example, if ‘a’ is replaced with ‘z’, then the cost of the operation is |”a”-“z”| = 25.
Examples:
Input: S = “abcdef”, K = 2
Output: 8
Explanation:
One possible answer is “cdcdcd” and the repeated k length substring is “cd”. The minimum cost required to convert the string is calculated 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
Naive Approach: The simplest approach is to generate all possible permutations of length K and find the cost to convert the given string such that it has a repeating pattern of length K. Then, print the minimum cost among them.
Time Complexity: O(N*K26), where N is the length of the given string and K is the given integer.
Auxiliary Space: O(N)
Efficient Approach: The idea is to use a Greedy Technique and observe that for any position i from 0 to K – 1, characters at position i + j * K must be the same where 0 ? j < N/K. For example, if S = “abcbbc” and K = 3 then, characters at positions 0 and 3 must be equal, characters at positions 1 and 4 must the same, and characters at positions 2 and 5 must be equal. Therefore, the minimum cost for characters at positions i + j * K can be calculated individually. Follow the steps below to solve the problem:
- Initialize a variable ans to store the minimum cost required.
- Traverse the string over the range [0, K – 1].
- For every position i, find the cost to place a character at positions i + j * K, for every character where 0 ? j < N/K. Calculate the minimum cost among them and update ans.
- After completing the above steps, print the value of ans as the required minimum cost.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minCost(string s, int k)
{
int n = s.size();
int ans = 0;
for ( int i = 0; i < k; i++)
{
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' ]++;
}
int min_cost = INT_MAX;
for ( int ch = 0; ch < 26; ch++)
{
int cost = 0;
for ( int tr = 0; tr < 26; tr++)
cost += abs (ch - tr) * a[tr];
min_cost = min(min_cost, cost);
}
ans += min_cost;
}
cout << (ans);
}
int main()
{
string S = "abcdefabc" ;
int K = 3;
minCost(S, K);
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
static void minCost(String s, int k)
{
int n = s.length();
int ans = 0 ;
for ( int i = 0 ; i < k; i++) {
int [] a = new int [ 26 ];
for ( int j = i; j < n; j += k) {
a[s.charAt(j) - 'a' ]++;
}
int min_cost
= Integer.MAX_VALUE;
for ( int ch = 0 ; ch < 26 ; ch++) {
int cost = 0 ;
for ( int tr = 0 ; tr < 26 ; tr++)
cost += Math.abs(ch - tr)
* a[tr];
min_cost = Math.min(min_cost,
cost);
}
ans += min_cost;
}
System.out.println(ans);
}
public static void main(String[] args)
{
String S = "abcdefabc" ;
int K = 3 ;
minCost(S, K);
}
}
|
Python3
import sys
def minCost(s, k):
n = len (s)
ans = 0
for i in range (k):
a = [ 0 ] * 26
for j in range (i, n, k):
a[ ord (s[j]) - ord ( 'a' )] + = 1
min_cost = sys.maxsize - 1
for ch in range ( 26 ):
cost = 0
for tr in range ( 26 ):
cost + = abs (ch - tr) * a[tr]
min_cost = min (min_cost,
cost)
ans + = min_cost
print (ans)
S = "abcdefabc"
K = 3
minCost(S, K)
|
C#
using System;
class GFG{
static void minCost( string s, int k)
{
int n = s.Length;
int ans = 0;
for ( int i = 0; i < k; i++)
{
int [] a = new int [26];
for ( int j = i; j < n; j += k)
{
a[s[j] - 'a' ]++;
}
int min_cost = Int32.MaxValue;
for ( int ch = 0; ch < 26; ch++)
{
int cost = 0;
for ( int tr = 0; tr < 26; tr++)
cost += Math.Abs(ch - tr) * a[tr];
min_cost = Math.Min(min_cost,
cost);
}
ans += min_cost;
}
Console.WriteLine(ans);
}
public static void Main()
{
string S = "abcdefabc" ;
int K = 3;
minCost(S, K);
}
}
|
Javascript
<script>
function minCost(s, k)
{
var n = s.length;
var ans = 0;
for ( var i = 0; i < k; i++)
{
var a = new Array(26).fill(0);
for ( var j = i; j < n; j += k)
{
a[s[j].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
var min_cost = 1000000000;
for ( var ch = 0; ch < 26; ch++)
{
var cost = 0;
for ( var tr = 0; tr < 26; tr++)
cost += Math.abs(ch - tr) * a[tr];
min_cost = Math.min(min_cost, cost);
}
ans += min_cost;
}
document.write(ans);
}
var S = "abcdefabc" ;
var K = 3;
minCost(S, K);
</script>
|
Time Complexity: O(N + K)
Auxiliary Space: O(N)
Last Updated :
20 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...