Minimum replacements such that the difference between the index of the same characters is divisible by 3

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find minimum replacements
// such that the difference between the
// index of the same characters
// is divisible by 3
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the number of
// minimal replacements
int countMinimalReplacements(string s)
{
  
    int n = s.length();
  
    int mini = INT_MAX;
    string dup = "012";
  
    // Generate all permutations
    do {
  
        // Count the replacements
        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 the replacements
    return mini;
}
  
// Driver Code
int main()
{
    string s = "2101200";
    cout << countMinimalReplacements(s);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find minimum replacements
// such that the difference between the
// index of the same characters
// is divisible by 3
class GFG {
  
    // Function to count the number of
    // minimal replacements
    static int countMinimalReplacements(String s)
    {
  
        int n = s.length();
  
        int mini = Integer.MAX_VALUE;
        char[] dup = "012".toCharArray();
  
        // Generate all permutations
        do {
  
            // Count the replacements
            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 the replacements
        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;
    }
  
    // Driver Code
    public static void main(String args[])
    {
        String s = "2101200";
        System.out.println(countMinimalReplacements(s));
    }
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find minimum replacements
// such that the difference between the
// index of the same characters
// is divisible by 3
using System;
  
class GFG {
  
    // Function to count the number of
    // minimal replacements
    static int countMinimalReplacements(String s)
    {
  
        int n = s.Length;
  
        int mini = int.MaxValue;
        char[] dup = "012".ToCharArray();
  
        // Generate all permutations
        do {
  
            // Count the replacements
            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 the replacements
        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;
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        String s = "2101200";
        Console.WriteLine(countMinimalReplacements(s));
    }
}
  
// This code has been contributed by 29AjayKumar

chevron_right


Output:

3


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : princiraj1992, 29AjayKumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.