📌  相关文章
📜  最小化所需的翻转,使字符串没有任何连续的 0 对

📅  最后修改于: 2021-09-06 17:44:49             🧑  作者: Mango

给定一个二进制字符串S ,任务是找到修改字符串所需的最小翻转次数,使其不包含任何连续的0对。

例子:

方法:该问题可以使用贪心技术解决。请按照以下步骤解决问题:

  • 遍历字符串的字符。对于每个i字符,检查S[i]S[i + 1]是否等于‘0’ 。如果发现为真,则增加 count 并将S[i + 1]更新为‘1’
  • 最后,打印获得的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find minimum flips required
// such that a string does not contain
// any pair of consecutive 0s
bool cntMinOperation(string S, int N)
{
 
    // Stores minimum count of flips
    int cntOp = 0;
 
    // Iterate over the characters
    // of the string
    for (int i = 0; i < N - 1; i++) {
 
        // If two consecutive characters
        // are equal to '0'
        if (S[i] == '0' && S[i + 1] == '0') {
 
            // Update S[i + 1]
            S[i + 1] = '1';
 
            // Update cntOp
            cntOp += 1;
        }
    }
 
    return cntOp;
}
 
// Driver Code
int main()
{
    string S = "10001";
    int N = S.length();
    cout << cntMinOperation(S, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to find minimum flips required
// such that a String does not contain
// any pair of consecutive 0s
static int cntMinOperation(char []S, int N)
{
 
    // Stores minimum count of flips
    int cntOp = 0;
 
    // Iterate over the characters
    // of the String
    for (int i = 0; i < N - 1; i++)
    {
 
        // If two consecutive characters
        // are equal to '0'
        if (S[i] == '0' && S[i + 1] == '0')
        {
 
            // Update S[i + 1]
            S[i + 1] = '1';
 
            // Update cntOp
            cntOp += 1;
        }
    }
    return cntOp;
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "10001";
    int N = S.length();
    System.out.print(cntMinOperation(S.toCharArray(), N));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to find minimum flips required
# such that a string does not contain
# any pair of consecutive 0s
def cntMinOperation(S, N):
 
    # Stores minimum count of flips
    cntOp = 0
 
    # Iterate over the characters
    # of the string
    for i in range(N - 1):
 
        # If two consecutive characters
        # are equal to '0'
        if (S[i] == '0' and S[i + 1] == '0'):
 
            # Update S[i + 1]
            S[i + 1] = '1'
 
            # Update cntOp
            cntOp += 1
    return cntOp
 
# Driver Code
if __name__ == '__main__':
    S = "10001"
    N = len(S)
    print(cntMinOperation([i for i in S], N))
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to find minimum flips required
  // such that a String does not contain
  // any pair of consecutive 0s
  static int cntMinOperation(char []S, int N)
  {
 
    // Stores minimum count of flips
    int cntOp = 0;
 
    // Iterate over the characters
    // of the String
    for (int i = 0; i < N - 1; i++)
    {
 
      // If two consecutive characters
      // are equal to '0'
      if (S[i] == '0' && S[i + 1] == '0')
      {
 
        // Update S[i + 1]
        S[i + 1] = '1';
 
        // Update cntOp
        cntOp += 1;
      }
    }
    return cntOp;
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    string S = "10001";
    int N = S.Length;
    Console.WriteLine(cntMinOperation(S.ToCharArray(), N));
  }
}
 
// This code is contributed by AnkThon


输出:
1

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live