Minimum count of distinct Strings by Rotating and Changing characters
Last Updated :
02 Aug, 2023
Given a string S of length N and an integer K. The task is to find the minimum count of distinct strings by changing at most K characters of the string (into any other lowercase letters) and by rotating the string left any number of times.
Examples:
Input : N = 4, S = “abac”, K = 1
Output: 2
Explanation: In the initial string, the number of distinct strings after left rotations is 4, i.e abac -> baca ->acab ->caba. On changing ‘c’ to ‘b’, the string becomes “abab”. For the updated string, the number of distinct strings after left rotations is 2, i.e. abab -> baba. It can be checked that this is the minimum possible number of distinct strings after left rotations by performing the given operation K(=1) times.
Input : N = 10, S = “cccccccccc”, K = 0
Output : 0
Approach: This can be solved with the following idea:
For string S, consider the smallest integer D, which is the divisor of S such that S[i] = S[i + D] =….= S[N – D + i] for all i = 1 to D. Since, this observation is always true for D = N, so such a D always exist. In such a case, the number of distinct strings achievable by left rotation would be D.
Follow the steps to solve the problem:
- Iterate through all the divisors of N.
- For the current divisor (say ‘l’), calculate the minimum number of characters in the string that need to be changed (say “now”) such that the string satisfies the condition mentioned in the observation.
- This can be easily calculated with the help of an array (say cnt[]) that stores the number of occurrences of each character in the string at a distance of ‘l’.
- If the calculated value of “now” is smaller than K, then return that, else proceed for the next iteration (i.e. check the same for the next divisor of N).
Below is the code based on the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
int minimizeNumberOfStrings( int N, int K, string S)
{
int ans = N;
for ( int l = 1; l <= N; l++)
if (N % l == 0) {
int now = 0;
for ( int i = 0; i < l; i++) {
int cnt[26] = {};
int mx = 0;
for ( int j = i; j < N; j += l)
mx = max(mx, ++cnt[S[j] - 'a' ]);
int curr = N / l;
now += curr - mx;
}
if (now <= K) {
ans = l;
break ;
}
}
return ans;
}
int32_t main()
{
int N = 4, K = 1;
string S = "abac" ;
int answer = minimizeNumberOfStrings(N, K, S);
cout << answer;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static long minimizeNumberOfStrings( int N, int K,
String S)
{
long ans = N;
for ( int l = 1 ; l <= N; l++) {
if (N % l == 0 ) {
long now = 0 ;
for ( int i = 0 ; i < l; i++) {
int [] cnt = new int [ 26 ];
int mx = 0 ;
for ( int j = i; j < N; j += l) {
mx = Math.max(
mx, ++cnt[S.charAt(j) - 'a' ]);
}
int curr = N / l;
now += curr - mx;
}
if (now <= K) {
ans = l;
break ;
}
}
}
return ans;
}
public static void main(String[] args)
{
int N = 4 , K = 1 ;
String S = "abac" ;
long answer = minimizeNumberOfStrings(N, K, S);
System.out.println(answer);
}
}
|
Python3
def minimizeNumberOfStrings(N, K, S):
ans = N
for l in range ( 1 , N / / 2 + 1 ):
if N % l = = 0 :
now = 0
for i in range (l):
cnt = [ 0 ] * 26
mx = 0
for j in range (i, N, l):
cnt[ ord (S[j]) - ord ( 'a' )] + = 1
mx = max (mx, cnt[ ord (S[j]) - ord ( 'a' )])
curr = N / / l
now + = curr - mx
if now < = K:
ans = l
break
return ans
if __name__ = = '__main__' :
N = 4
K = 1
S = "abac"
answer = minimizeNumberOfStrings(N, K, S)
print (answer)
|
C#
using System;
class GFG {
static long MinimizeNumberOfStrings( int N, int K,
string S)
{
int ans = N;
for ( int l = 1; l <= N; l++) {
if (N % l == 0) {
int now = 0;
for ( int i = 0; i < l; i++) {
int [] cnt = new int [26];
int mx = 0;
for ( int j = i; j < N; j += l) {
mx = Math.Max(mx,
++cnt[S[j] - 'a' ]);
}
int curr = N / l;
now += curr - mx;
}
if (now <= K) {
ans = l;
break ;
}
}
}
return ans;
}
static void Main( string [] args)
{
int N = 4, K = 1;
string S = "abac" ;
int answer = ( int )MinimizeNumberOfStrings(N, K, S);
Console.WriteLine(answer);
}
}
|
Javascript
function minimizeNumberOfStrings(N, K, S) {
let ans = N;
for (let l = 1; l <= N; l++) {
if (N % l == 0) {
let now = 0;
for (let i = 0; i < l; i++) {
let cnt = new Array(26).fill(0);
let mx = 0;
for (let j = i; j < N; j += l) {
mx = Math.max(mx, ++cnt[S.charCodeAt(j) - 'a' .charCodeAt()]);
}
let curr = Math.floor(N / l);
now += curr - mx;
}
if (now <= K) {
ans = l;
break ;
}
}
}
return ans;
}
let N = 4, K = 1;
let S = "abac" ;
let answer = minimizeNumberOfStrings(N, K, S);
console.log(answer);
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...