📌  相关文章
📜  使二进制字符串交替的最小替换数套装2

📅  最后修改于: 2021-04-26 10:10:37             🧑  作者: Mango

给定二进制字符串str ,任务是在字符串中查找要替换的最小字符数,以使字符串交替出现(即格式为01010101…10101010… )。
例子:

我们已经在“翻转次数”中讨论了一种使二进制字符串交替的方法。在这篇文章中,讨论了一种更好的方法。
方法:对于字符串str ,可以有两种可能的解决方案。结果字符串可以是

  1. 010101…
  2. 101010…

为了找到最小的替换,计算替换的数量以将字符串转换为类型1并将其存储在count中,然后最小替换将是min(count,len – count) ,其中len是字符串的长度。 len – count是将类型2转换为字符串的替换次数。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimum number of
// characters of the given binary string
// to be replaced to make the string alternating
int minReplacement(string s, int len)
{
    int ans = 0;
    for (int i = 0; i < len; i++) {
 
        // If there is 1 at even index positions
        if (i % 2 == 0 && s[i] == '1')
            ans++;
 
        // If there is 0 at odd index positions
        if (i % 2 == 1 && s[i] == '0')
            ans++;
    }
    return min(ans, len - ans);
}
 
// Driver code
int main()
{
    string s = "1100";
    int len = s.size();
    cout << minReplacement(s, len);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function to return the minimum number of
    // characters of the given binary string
    // to be replaced to make the string alternating
    static int minReplacement(String s, int len)
    {
        int ans = 0;
        for (int i = 0; i < len; i++) {
 
            // If there is 1 at even index positions
            if (i % 2 == 0 && s.charAt(i) == '1')
                ans++;
 
            // If there is 0 at odd index positions
            if (i % 2 == 1 && s.charAt(i) == '0')
                ans++;
        }
        return Math.min(ans, len - ans);
    }
 
    // Driver code
    public static void main(String args[])
    {
        String s = "1100";
        int len = s.length();
        System.out.print(minReplacement(s, len));
    }
}


Python3
# Python3 implementation of the approach.
 
# Function to return the minimum number of
# characters of the given binary string
# to be replaced to make the string alternating
def minReplacement(s, length):
 
    ans = 0
    for i in range(0, length):
 
        # If there is 1 at even index positions
        if i % 2 == 0 and s[i] == '1':
            ans += 1
 
        # If there is 0 at odd index positions
        if i % 2 == 1 and s[i] == '0':
            ans += 1
     
    return min(ans, length - ans)
 
# Driver code
if __name__ == "__main__":
 
    s = "1100"
    length = len(s)
    print(minReplacement(s, length))
         
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the minimum number of
    // characters of the given binary string
    // to be replaced to make the string alternating
    static int minReplacement(String s, int len)
    {
        int ans = 0;
        for (int i = 0; i < len; i++)
        {
 
            // If there is 1 at even index positions
            if (i % 2 == 0 && s[i] == '1')
                ans++;
 
            // If there is 0 at odd index positions
            if (i % 2 == 1 && s[i] == '0')
                ans++;
        }
        return Math.Min(ans, len - ans);
    }
 
    // Driver code
    public static void Main(String []args)
    {
        String s = "1100";
        int len = s.Length;
        Console.Write(minReplacement(s, len));
    }
}
 
// This code contributed by Rajput-Ji


PHP


Javascript


输出:
2

时间复杂度: O(len)其中len是给定字符串的长度。