📌  相关文章
📜  最小替换数,使得相同字符的索引之间的差异可被3整除

📅  最后修改于: 2021-06-27 00:16:38             🧑  作者: Mango

给定字符串“ 0”,“ 1”和“ 2”。任务是找到字符串的最小替换,以使相同字符的索引之间的差异可被3整除。
例子:

方法:可以有6个不同的字符串,以使相似字符的索引之间的差异可被3整除。因此,生成所有6个不同的字符串,并将与原始字符串进行的替换进行比较。存储替换次数最少的字符串。可以使用C++中的next_permutation生成不同的字符串。
下面是上述方法的实现:

C++
// C++ program to find minimum replacements
// such that the difference between the
// index of the same characters
// is divisible by 3
#include 
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;
}


Java
// 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 */


C#
// 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


Javascript


输出:
3

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。