📌  相关文章
📜  最小化位替换,使 01 子串的计数等于 10 子串

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

最小化位替换,使 01 子串的计数等于 10 子串

给定一个二进制字符串str 。任务是最小化“0”被“1”“1”被“0”替换的次数,以平衡二进制字符串。一个二进制字符串被称为是平衡的: “如果“01”子串的数量=“10”子串的数量”。

例子:

方法:人们可以注意到“平衡的二进制字符串总是让它的第一个字符等于字符串的最后一个字符”。
只需要一步来平衡它,即s.back() = s.front()。请参阅下面提供的证明

下面是上述方法的实现。

C++
// C++ code to implement above approach
#include 
using namespace std;
 
// Function to count the minimum
// number of replacements
int minimizeReplacements(string str)
{
    unordered_map count;
    string temp;
     
    // Loop to count the minimum number
    // of replacements required
    for (int i = 0; i <
        str.length() - 1; i++) {
        temp = str.substr(i, 2);
        count[temp]++;
    }
 
    return abs(count["10"] - count["01"]);
}
 
// Driver code
int main()
{
    // Given string
    string str = "101010";
    cout << minimizeReplacements(str) << endl;
    return 0;
}


Java
// Java code to implement above approach
import java.util.HashMap;
 
class GFG{
 
// Function to count the minimum
// number of replacements
static int minimizeReplacements(String str)
{
    HashMap count = new HashMap();
    String temp;
 
    // Loop to count the minimum number
    // of replacements required
    for(int i = 0; i < str.length() - 1; i++)
    {
        temp = str.substring(i, i + 2);
        if (count.containsKey(temp))
            count.put(temp, count.get(temp) + 1);
        else
            count.put(temp, 1);
    }
    return Math.abs(count.get("10") - count.get("01"));
}
 
// Driver code
public static void main(String args[])
{
     
    // Given string
    String str = "101010";
    System.out.print(minimizeReplacements(str));
}
}
 
// This code is contributed by gfgking


Python3
# Python code for the above approach
 
# Function to count the minimum
# number of replacements
def minimizeReplacements(str):
    count = {}
    temp = ""
 
   # Loop to count the minimum number
   # of replacements required
    for i in range(0, len(str)-1):
        temp = str[i: i+2]
        if temp in count:
            count[temp] = count[temp] + 1
        else:
            count[temp] = 1
    return abs(count["10"] - count["01"])
 
    # Driver code
 
    # Given string
str = "101010"
print(minimizeReplacements(str))
 
# This code is contributed by Potta Lokesh


C#
// C# code to implement above approach
using System;
using System.Collections.Generic;
class GFG
{
   
    // Function to count the minimum
    // number of replacements
    static int minimizeReplacements(string str)
    {
        Dictionary count
            = new Dictionary();
        string temp;
 
        // Loop to count the minimum number
        // of replacements required
        for (int i = 0; i < str.Length - 1; i++) {
            temp = str.Substring(i, 2);
            if (count.ContainsKey(temp))
                count[temp]++;
            else
                count[temp] = 1;
        }
 
        return Math.Abs(count["10"] - count["01"]);
    }
 
    // Driver code
    public static void Main()
    {
        // Given string
        string str = "101010";
        Console.WriteLine(minimizeReplacements(str));
    }
}
 
// This code is contributed by ukasp.


Javascript


输出
1

时间复杂度: O(N)
辅助空间: O(1)