Minimum replacements such that the difference between the index of the same characters is divisible by 3
Last Updated :
11 Jan, 2023
Given a string of ‘0’, ‘1’ and ‘2’. The task is to find the minimum replacements in the string such that the differences between the indexes of the same characters is divisible by 3.
Examples:
Input: s = “2101200”
Output: 3
1201201 or 2102102 can be the resultant string
which has 3 replacements.
Input: s = “012”
Output: 0
Approach: There can be 6 different strings such that the difference between the index of similar characters is divisible by 3. Hence generate all 6 different strings, and compare the replacements done with the original string. Store the string which has a minimal number of replacements. The different strings can be generated using next_permutation in C++.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countMinimalReplacements(string s)
{
int n = s.length();
int mini = INT_MAX;
string dup = "012" ;
do {
int dif = 0;
for ( int i = 0; i < n; i++)
if (s[i] != dup[i % 3])
dif++;
mini = min(mini, dif);
} while (next_permutation(dup.begin(), dup.end()));
return mini;
}
int main()
{
string s = "2101200" ;
cout << countMinimalReplacements(s);
return 0;
}
|
Java
class GFG {
static int countMinimalReplacements(String s)
{
int n = s.length();
int mini = Integer.MAX_VALUE;
char [] dup = "012" .toCharArray();
do {
int dif = 0 ;
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) != dup[i % 3 ]) {
dif++;
}
}
mini = Math.min(mini, dif);
} while (next_permutation(dup));
return mini;
}
static boolean next_permutation( char [] p)
{
for ( int a = p.length - 2 ; a >= 0 ; --a) {
if (p[a] < p[a + 1 ]) {
for ( int b = p.length - 1 ;; --b) {
if (p[b] > p[a]) {
char t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1 ; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
}
}
}
return false ;
}
public static void main(String args[])
{
String s = "2101200" ;
System.out.println(countMinimalReplacements(s));
}
}
|
Python3
import sys
def countMinimalReplacements(s):
n = len (s)
mini = sys.maxsize
dup = "012"
for i in range ( 6 ):
dif = 0
for j in range (n):
if s[j] ! = dup[j % 3 ]:
dif + = 1
mini = min (mini, dif)
dup = dup[ 1 :] + dup[ 0 ]
return mini
if __name__ = = '__main__' :
s = "2101200"
print (countMinimalReplacements(s))
|
C#
using System;
class GFG {
static int countMinimalReplacements(String s)
{
int n = s.Length;
int mini = int .MaxValue;
char [] dup = "012" .ToCharArray();
do {
int dif = 0;
for ( int i = 0; i < n; i++) {
if (s[i] != dup[i % 3]) {
dif++;
}
}
mini = Math.Min(mini, dif);
} while (next_permutation(dup));
return mini;
}
static bool next_permutation( char [] p)
{
for ( int a = p.Length - 2; a >= 0; --a) {
if (p[a] < p[a + 1]) {
for ( int b = p.Length - 1;; --b) {
if (p[b] > p[a]) {
char t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.Length - 1; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
}
}
}
return false ;
}
public static void Main(String[] args)
{
String s = "2101200" ;
Console.WriteLine(countMinimalReplacements(s));
}
}
|
Javascript
<script>
function countMinimalReplacements(s) {
var n = s.length;
var mini = 2147483647;
var dup = "012" .split( "" );
do {
var dif = 0;
for ( var i = 0; i < n; i++) {
if (s[i] !== dup[i % 3]) {
dif++;
}
}
mini = Math.min(mini, dif);
} while (next_permutation(dup));
return mini;
}
function next_permutation(p) {
for ( var a = p.length - 2; a >= 0; --a) {
if (p[a] < p[a + 1]) {
for ( var b = p.length - 1; ; --b) {
if (p[b] > p[a]) {
var t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
}
}
}
return false ;
}
var s = "2101200" ;
document.write(countMinimalReplacements(s));
</script>
|
Complexity Analysis:
- Time Complexity: O(3*N), as we are a loop to traverse N times. Where n is the length of the string.
- Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...