📌  相关文章
📜  要插入的最小字符数,以使三个连续字符都不相同

📅  最后修改于: 2022-05-13 01:57:08.114000             🧑  作者: Mango

要插入的最小字符数,以使三个连续字符都不相同

给定一个字符串str ,任务是修改字符串,使三个连续的字符都不相同。在单个操作中,可以在字符串的任何位置插入任何字符。找出所需的此类操作的最小数量。
例子:

方法:对于每三个相同的连续字符,必须在它们之间插入一个字符以使任何三个连续字符不同。但是需要尽量减少操作次数,因此必须在第二个字符之后插入字符。例如,如果字符串是“bbbb”,那么如果字符被插入到第二个位置,即“babbb”,那么仍然有三个连续的相同字符,需要另一个操作来解决这个问题,但是如果字符被插入到第三个位置即“bbabb”,那么只需要一个操作。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of characters
// that are to be inserted in str such that no
// three consecutive characters are same
int getCount(string str, int n)
{
 
    // To store the count of
    // operations required
    int cnt = 0;
 
    int i = 0;
    while (i < n - 2) {
 
        // A character needs to be
        // inserted after str[i + 1]
        if (str[i] == str[i + 1]
            && str[i] == str[i + 2]) {
            cnt++;
            i = i + 2;
        }
 
        // Current three consecutive
        // characters are not same
        else
            i++;
    }
 
    return cnt;
}
 
// Driver code
int main()
{
    string str = "aabbbcc";
    int n = str.length();
 
    cout << getCount(str, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
     
    // Function to return the count of characters
    // that are to be inserted in str such that no
    // three consecutive characters are same
    static int getCount(char[] str, int n)
    {
 
        // To store the count of
        // operations required
        int cnt = 0;
 
        int i = 0;
        while (i < n - 2)
        {
 
            // A character needs to be
            // inserted after str[i + 1]
            if (str[i] == str[i + 1] &&
                str[i] == str[i + 2])
            {
                cnt++;
                i = i + 2;
            }
             
            // Current three consecutive
            // characters are not same
            else
            {
                i++;
            }
        }
        return cnt;
    }
 
    // Driver code
    static public void main(String[] arg)
    {
        String str = "aabbbcc";
        int n = str.length();
 
        System.out.println(getCount(str.toCharArray(), n));
    }
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
 
# Function to return the count of characters
# that are to be inserted in str1 such that no
# three consecutive characters are same
def getCount(str1, n):
 
    # To store the count of
    # operations required
    cnt = 0;
 
    i = 0;
    while (i < n - 2):
 
        # A character needs to be
        # inserted after str1[i + 1]
        if (str1[i] == str1[i + 1] and
            str1[i] == str1[i + 2]):
            cnt += 1
            i = i + 2
 
        # Current three consecutive
        # characters are not same
        else:
            i += 1
 
    return cnt
 
# Driver code
str1 = "aabbbcc"
n = len(str1)
 
print(getCount(str1, n))
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the above approach
using System;
 
class GFG
{
     
    // Function to return the count of characters
    // that are to be inserted in str such that no
    // three consecutive characters are same
    static int getCount(string str, int n)
    {
 
        // To store the count of
        // operations required
        int cnt = 0;
 
        int i = 0;
        while (i < n - 2)
        {
 
            // A character needs to be
            // inserted after str[i + 1]
            if (str[i] == str[i + 1] &&
                str[i] == str[i + 2])
            {
                cnt++;
                i = i + 2;
            }
             
            // Current three consecutive
            // characters are not same
            else
            {
                i++;
            }
        }
        return cnt;
    }
 
    // Driver code
    static public void Main ()
    {
        string str = "aabbbcc";
        int n = str.Length;
 
        Console.WriteLine(getCount(str, n));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
1