📌  相关文章
📜  检查是否可以通过更改与 1 相邻的 0 来增加二进制字符串中 1 的计数

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

检查是否可以通过更改与 1 相邻的 0 来增加二进制字符串中 1 的计数

给定一个大小为N的二进制字符串S ,任务是通过将与1相邻的0更改为任何其他字符来检查1的计数是否可以大于0的计数。如果可能,则打印Yes 。否则,打印No

注意:任何具有1的索引最多只能选择一次。

例子:

方法:给定的问题可以通过计算字符串S10的数量来解决,然后在遍历字符串时,如果任何索引i的值为'1'并且如果左侧或右侧(不是两者) 为'0'然后将其更改为'_' 。该值更改为“_”而不是“1” ,因此不再使用它。更改值后,将0 的计数减少1 。请按照以下步骤解决问题:

  • 初始化两个变量,例如cnt0cnt10以存储0s1s的计数。
  • 遍历字符串S并将10的计数分别存储在变量cnt0cnt1中。
  • 从左侧遍历字符串S并检查当前字符是否为1如果发现为真则检查条件:
    • if(i > 0 && S[i – 1] == '0')如果发现为真则将左侧相邻的 0 更改为S[i-1] = '_'并将cnt0的值减1 .
    • else if(i < S.length() && S[i+1] == '0')如果发现为真则将右边的 0 改为S[i+1] = '_'并递减值cnt01
  • 完成上述步骤后,如果(cnt1 > cnt0)的值,则打印“Yes” 。否则,打印“否”

以下是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check whether in a given
// binary string can we make number of
// 1's greater than the  number of 0's
// by doing the given operation
void isOnesGreater(string S, int N)
{
 
    // Stores the count of 0's
    int cnt0 = 0;
 
    // Stores the count of 1's
    int cnt1 = 0;
 
    // Traverse through the string S
    for (int i = 0; i < N; i++) {
 
        // Check current character is 1
        if (S[i] == '1')
 
            // Update cnt1
            cnt1++;
 
        else
            // Update cnt0
            cnt0++;
    }
 
    // Traverse through the string S
    for (int i = 0; i < N; i++) {
 
        // Check curretn character is 1
        if (S[i] == '1') {
 
            // Check if left adjacent
            // character is 0
            if (i > 0 && S[i - 1] == '0') {
 
                // Change the left adjacent
                // character to _
                S[i - 1] = '_';
 
                // Update the cnt0
                cnt0--;
            }
 
            // Check if right adjacent
            // character is 0
            else if (i < N && S[i + 1] == '0') {
 
                // Change the right adjacent
                // character to _
                S[i + 1] = '_';
 
                // Update the cnt0
                cnt0--;
            }
        }
    }
 
    // Check count of 1's is greater
    // than the count of 0's
    if (cnt1 > cnt0) {
 
        cout << "Yes";
    }
    else {
 
        cout << "No";
    }
}
 
// Driver Code
int main()
{
 
    string S = "01";
    int N = S.length();
 
    isOnesGreater(S, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to check whether in a given
    // binary string can we make number of
    // 1's greater than the  number of 0's
    // by doing the given operation
    static void isOnesGreater(String S, int N)
    {
        char[] st = new char[S.length()];
   
        // Copy character by character into array
        for (int i = 0; i < S.length(); i++) {
            st[i] = S.charAt(i);
        }
   
        // Stores the count of 0's
        int cnt0 = 0;
 
        // Stores the count of 1's
        int cnt1 = 0;
 
        // Traverse through the string S
        for (int i = 0; i < N; i++) {
 
            // Check current character is 1
            if (st[i] == '1')
 
                // Update cnt1
                cnt1++;
 
            else
                // Update cnt0
                cnt0++;
        }
 
        // Traverse through the string S
        for (int i = 0; i < N; i++) {
 
            // Check curretn character is 1
            if (st[i] == '1') {
 
                // Check if left adjacent
                // character is 0
                if (i > 0 && st[i - 1] == '0') {
 
                    // Change the left adjacent
                    // character to _
                    st[i - 1] = '_';
 
                    // Update the cnt0
                    cnt0--;
                }
 
                // Check if right adjacent
                // character is 0
                else if (i < N && st[i + 1] == '0') {
 
                    // Change the right adjacent
                    // character to _
                    st[i + 1] = '_';
 
                    // Update the cnt0
                    cnt0--;
                }
            }
        }
 
        // Check count of 1's is greater
        // than the count of 0's
        if (cnt1 > cnt0) {
 
            System.out.println("Yes");
        }
        else {
 
            System.out.println("No");
        }
    }
 
    // Driver Code
    public static void main(String args[])
    {
 
        String S = "01";
        int N = S.length();
 
        isOnesGreater(S, N);
    }
}
 
// This code is contributed by bgangwar59.


Python3
# Python Program to implement
# the above approach
 
# Function to check whether in a given
# binary string can we make number of
# 1's greater than the  number of 0's
# by doing the given operation
def isOnesGreater(S, N):
    S = list(S)
     
    # Stores the count of 0's
    cnt0 = 0
 
    # Stores the count of 1's
    cnt1 = 0
 
    # Traverse through the string S
    for i in range(N):
 
        # Check current character is 1
        if (S[i] == '1'):
 
            # Update cnt1
            cnt1 += 1
 
        else:
            # Update cnt0
            cnt0 += 1
     
    # Traverse through the string S
    for i in range(N):
 
        # Check curretn character is 1
        if (S[i] == '1'):
 
            # Check if left adjacent
            # character is 0
            if (i > 0 and S[i - 1] == '0'):
 
                # Change the left adjacent
                # character to _
                S[i - 1] = '_'
 
                # Update the cnt0
                cnt0 -= 1
             
            # Check if right adjacent
            # character is 0
            elif (i < N and S[i + 1] == '0'):
 
                # Change the right adjacent
                # character to _
                S[i + 1] = '_'
 
                # Update the cnt0
                cnt0 -= 1
 
    # Check count of 1's is greater
    # than the count of 0's
    if (cnt1 > cnt0):
        print("Yes")
    else:
        print("No")
 
# Driver Code
S = "01"
N = len(S)
 
isOnesGreater(S, N)
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
using System.Text;
class GFG {
 
    // Function to check whether in a given
    // binary string can we make number of
    // 1's greater than the  number of 0's
    // by doing the given operation
    static void isOnesGreater(string S, int N)
    {
        StringBuilder st = new StringBuilder(S);
        // Stores the count of 0's
        int cnt0 = 0;
 
        // Stores the count of 1's
        int cnt1 = 0;
 
        // Traverse through the string S
        for (int i = 0; i < N; i++) {
 
            // Check current character is 1
            if (st[i] == '1')
 
                // Update cnt1
                cnt1++;
 
            else
                // Update cnt0
                cnt0++;
        }
 
        // Traverse through the string S
        for (int i = 0; i < N; i++) {
 
            // Check curretn character is 1
            if (st[i] == '1') {
 
                // Check if left adjacent
                // character is 0
                if (i > 0 && st[i - 1] == '0') {
 
                    // Change the left adjacent
                    // character to _
                    st[i - 1] = '_';
 
                    // Update the cnt0
                    cnt0--;
                }
 
                // Check if right adjacent
                // character is 0
                else if (i < N && st[i + 1] == '0') {
 
                    // Change the right adjacent
                    // character to _
                    st[i + 1] = '_';
 
                    // Update the cnt0
                    cnt0--;
                }
            }
        }
 
        // Check count of 1's is greater
        // than the count of 0's
        if (cnt1 > cnt0) {
 
            Console.WriteLine("Yes");
        }
        else {
 
            Console.WriteLine("No");
        }
    }
 
    // Driver Code
    public static void Main()
    {
 
        string S = "01";
        int N = S.Length;
 
        isOnesGreater(S, N);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
Yes

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