📌  相关文章
📜  检查 K '0's 是否可以翻转,使得二进制字符串不包含相邻的 '1' 对

📅  最后修改于: 2021-09-03 03:11:08             🧑  作者: Mango

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

方法:遍历字符串并将那些 ‘ 0′替换为 ‘ 1′的想法,其中两个相邻字符都是 ‘ 0′并翻转其中一个 ‘ 0′以计算翻转的所有可能位置,这样它就不会’ t 影响原始字符串。请按照以下步骤解决问题:

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

下面是上述方法的实现:

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)

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