📌  相关文章
📜  检查是否可以翻转K’0’,以便二进制字符串不包含一对相邻的’1′

📅  最后修改于: 2021-04-17 16:09:46             🧑  作者: Mango

给定长度为N且整数K的二进制字符串S ,任务是检查是否有可能翻转K 0 ,以使所得的字符串不包含任何一对相邻的1 。如果可能的话,请打印“是” 。否则,打印“否”

方法:遍历字符串并用两个相邻字符均为“ 0”的1”替换那些“ 0” ,然后翻转“ 0”之一以计算翻转的所有可能位置,这样就可以了。 t影响原始字符串。请按照以下步骤解决问题:

  • 初始化s变量,例如cnt0,以存储可以翻转的位置数。
  • 使用变量i遍历字符串并执行以下步骤:
    • 如果当前字符为“ 1” ,则将i2
    • 除此以外:
      • 如果i = 0,并且s [i + 1] =’0’:将cnt1并将i2 。否则,将i增加1
      • 如果i =(N – 1),并且s [i – 1] =’0’:将cnt递增1i递增2 。否则,将i增加1
      • 否则,如果s [i + 1] =’0’且s [i – 1] =’0’:将cnt递增1i递增2。否则,将i递增1
  • 完成上述步骤后,如果cnt的值至少为K,则打印“是” ,否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if k '0's can
// be flipped such that the string
// does not contain any pair of adjacent '1's
void canPlace(string s, int n, int k)
{
    // Store the count of flips
    int cnt = 0;
 
    // Variable to iterate the string
    int i = 0;
 
    // Iterate over characters
    // of the string
    while (i < n) {
 
        // If the current character
        // is '1', increment i by 2
        if (s[i] == '1') {
            i += 2;
        }
 
        // Otherwise, 3 cases arises
        else {
 
            // If the current index
            // is the starting index
            if (i == 0) {
 
                // If next character is '0'
                if (s[i + 1] == '0') {
                    cnt++;
                    i += 2;
                }
 
                // Increment i by 1
                else
                    i++;
            }
 
            // If the current index
            // is the last index
            else if (i == n - 1) {
 
                // If previous character is '0'
                if (s[i - 1] == '0') {
 
                    cnt++;
                    i += 2;
                }
                else
                    i++;
            }
 
            // For remaining characters
            else {
 
                // If both the adjacent
                // characters are '0'
                if (s[i + 1] == '0' && s[i - 1] == '0') {
 
                    cnt++;
                    i += 2;
                }
                else
                    i++;
            }
        }
    }
 
    // If cnt is at least K, print "Yes"
    if (cnt >= k) {
        cout << "Yes";
    }
 
    // Otherwise, print "No"
    else {
        cout << "No";
    }
}
 
// Driver Code
int main()
{
    string S = "10001";
    int K = 1;
    int N = S.size();
 
    canPlace(S, N, K);
 
    return 0;
}


Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG
{
 
  // Function to check if k '0's can
  // be flipped such that the string
  // does not contain any pair of adjacent '1's
  public static void canPlace(String s, int n, int k)
  {
 
    // Store the count of flips
    int cnt = 0;
 
    // Variable to iterate the string
    int i = 0;
 
    // Iterate over characters
    // of the string
    while (i < n)
    {
 
      // If the current character
      // is '1', increment i by 2
      if (s.charAt(i) == '1')
      {
        i += 2;
      }
 
      // Otherwise, 3 cases arises
      else
      {
 
        // If the current index
        // is the starting index
        if (i == 0)
        {
 
          // If next character is '0'
          if (s.charAt(i + 1) == '0')
          {
            cnt++;
            i += 2;
          }
 
          // Increment i by 1
          else
            i++;
        }
 
        // If the current index
        // is the last index
        else if (i == n - 1)
        {
 
          // If previous character is '0'
          if (s.charAt(i - 1) == '0')
          {
 
            cnt++;
            i += 2;
          }
          else
            i++;
        }
 
        // For remaining characters
        else
        {
 
          // If both the adjacent
          // characters are '0'
          if (s.charAt(i + 1) == '0' && s.charAt(i - 1) == '0')
          {
 
            cnt++;
            i += 2;
          }
          else
            i++;
        }
      }
    }
 
    // If cnt is at least K, print "Yes"
    if (cnt >= k)
    {
      System.out.println("Yes");
    }
 
    // Otherwise, print "No"
    else
    {
      System.out.println("No");
    }
  }
 
  // Driver code
  public static void main (String[] args)
  {
    String S = "10001";
    int K = 1;
    int N = 5;
    canPlace(S, N, K);
  }
}
 
// This code is contributed by manupatharia.


Python3
# Python3 program for the above approach
 
# Function to check if k '0's can
# be flipped such that the string
# does not contain any pair of adjacent '1's
def canPlace(s, n, k) :
 
    # Store the count of flips
    cnt = 0
     
    # Variable to iterate the string
    i = 0
     
    # Iterate over characters
    # of the string
    while (i < n) :
     
      # If the current character
      # is '1', increment i by 2
      if (s[i] == '1') :
        i += 2
     
      # Otherwise, 3 cases arises
      else :
     
        # If the current index
        # is the starting index
        if (i == 0) :
     
          # If next character is '0'
          if (s[i + 1] == '0') :
            cnt += 1
            i += 2
     
          # Increment i by 1
          else :
            i += 1
     
        # If the current index
        # is the last index
        elif (i == n - 1) :
     
          # If previous character is '0'
          if (s[i - 1] == '0') :
     
            cnt += 1
            i += 2
           
          else :
            i += 1
     
        # For remaining characters
        else :
     
          # If both the adjacent
          # characters are '0'
          if (s[i + 1] == '0' and s[i - 1] == '0') :
 
            cnt += 1
            i += 2
           
          else :
            i += 1
     
    # If cnt is at least K, print "Yes"
    if (cnt >= k) :
     
      print("Yes")
     
    # Otherwise, print "No"
    else :
     
      print("No")
       
      # Driver code
S = "10001"
K = 1
N = len(S)
 
canPlace(S, N, K)
 
# This code is contributed by divyeshrabadiya07.


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to check if k '0's can
  // be flipped such that the string
  // does not contain any pair of adjacent '1's
  static void canPlace(string s, int n, int k)
  {
 
    // Store the count of flips
    int cnt = 0;
 
    // Variable to iterate the string
    int i = 0;
 
    // Iterate over characters
    // of the string
    while (i < n) {
 
      // If the current character
      // is '1', increment i by 2
      if (s[i] == '1') {
        i += 2;
      }
 
      // Otherwise, 3 cases arises
      else {
 
        // If the current index
        // is the starting index
        if (i == 0) {
 
          // If next character is '0'
          if (s[i + 1] == '0') {
            cnt++;
            i += 2;
          }
 
          // Increment i by 1
          else
            i++;
        }
 
        // If the current index
        // is the last index
        else if (i == n - 1) {
 
          // If previous character is '0'
          if (s[i - 1] == '0') {
 
            cnt++;
            i += 2;
          }
          else
            i++;
        }
 
        // For remaining characters
        else {
 
          // If both the adjacent
          // characters are '0'
          if (s[i + 1] == '0' && s[i - 1] == '0') {
 
            cnt++;
            i += 2;
          }
          else
            i++;
        }
      }
    }
 
    // If cnt is at least K, print "Yes"
    if (cnt >= k)
    {
      Console.WriteLine("Yes");
    }
 
    // Otherwise, print "No"
    else
    {
      Console.WriteLine("No");
    }
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    string S = "10001";
    int K = 1;
    int N = S.Length;
 
    canPlace(S, N, K);
  }
}
 
// This code is contributed by susmitakundugoaldanga.


输出:
Yes

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