📌  相关文章
📜  生成0和1的连续子串所需的最小翻转

📅  最后修改于: 2021-04-22 06:41:32             🧑  作者: Mango

给定长度为N的二进制字符串S ,任务是找到转换给定字符串所需的最小位翻转次数,以使其仅包含0和1的连续子字符串,以使最终字符串的形式为000。 000,111..111,111 … 000000 … 111。
例子:

方法:
可以在两个线性遍历中有效地计算最小翻转次数。
在第一个遍历中,我们将计算最坏情况下所需的最小翻转次数,因为它可以等于初始总0数。
在第二次遍历中,在每一步中,所需的翻转总数将为该点之前的总1和该点之后的总0。我们将取每一步中计算出的所有值中的最小值。
因此,要解决此问题,请按照以下步骤操作:

  • 初始化变量count0 = 0, count1 = 0和res =0。其中,count0计数为0,count1存储计数为1,res存储所需的位翻转。
  • 遍历输入字符串,计算0并将其存储在res变量中。
  • 遍历输入字符串,如果找到字符0,则减去计数0,并将字符1的计数存储在变量count1中,并将res更新为min(res,count0 + count1)

下面是上述方法的实现。

C++
// C++ implementation of the above approach
  
#include 
using namespace std;
  
int minChanges(string str, int N)
{
    int res;
    int count0 = 0, count1 = 0;
  
    // Traverse input string
    // and store the count of 0
    for (char x : str) {
        count0 += (x == '0');
    }
    res = count0;
  
    // Traverse the input string again
    // to find minimum number of flips
    for (char x : str) {
        count0 -= (x == '0');
        count1 += (x == '1');
        res = min(res, count1 + count0);
    }
  
    return res;
}
  
// Driver code
int main()
{
    int N = 9;
    string str = "000101001";
  
    cout << minChanges(str, N);
    return 0;
}


Java
// Java implementation of the above approach
import java.io.*;
  
class GFG{
  
static int minChanges(String str, int N)
{
    int res;
    int count0 = 0, count1 = 0;
  
    // Traverse input string
    // and store the count of 0
    for(char x : str.toCharArray()) 
    {
       if (x == '0')
           count0++;
    }
    res = count0;
  
    // Traverse the input string again
    // to find minimum number of flips
    for(char x : str.toCharArray()) 
    {
       if (x == '0')
           count0--;
       if (x == '1')
           count1++;
             
       res = Math.min(res, count1 + count0);
    }
    return res;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 9;
    String str = "000101001";
  
    System.out.println(minChanges(str, N));
}
}
  
// This code is contributed by offbeat


Python3
# Python3 implementation of the above approach
def minChanges(str, N):
      
    count0 = 0
    count1 = 0
      
    # Traverse input string
    # and store the count of 0
    for x in str:
        count0 += (x == '0')
  
    res = count0
              
    # Traverse the input string again
    # to find minimum number of flips
    for x in str:
        count0 -= (x == '0')
        count1 += (x == '1')
        res = min(res, count1 + count0)
          
    return res
  
  
# Driver code
N = 9
str = "000101001"
  
print(minChanges(str, N))
  
# This code is contributed by shubhamsingh10


C#
// C# implementation of the above approach
using System;
  
class GFG{
  
static int minChanges(String str, int N)
{
    int res;
    int count0 = 0, count1 = 0;
  
    // Traverse input string
    // and store the count of 0
    for(int i = 0; i < str.Length; i++) 
    {
        if (str[i] == '0')
            count0++;
    }
    res = count0;
  
    // Traverse the input string again
    // to find minimum number of flips
    for(int i = 0; i< str.Length; i++) 
    {
        if (str[i] == '0')
            count0--;
        if (str[i] == '1')
            count1++;
                  
        res = Math.Min(res, count1 + count0);
    }
    return res;
}
  
// Driver code
public static void Main()
{
    int N = 9;
    String str = "000101001";
  
    Console.Write(minChanges(str, N));
}
}
  
// This code is contributed by chitranayal


输出:
2

时间复杂度: O(k) ,其中k是二进制字符串的长度。
空间复杂度: O(1)