Minimum replacements such that the difference between the index of the same characters is divisible by 3 | C ++ program to find minimum replacements such that the difference between the index of the same characters is divisible by 3 ; Function to count the number of minimal replacements ; Generate all permutations ; Count the replacements ; Return the replacements ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }