📌  相关文章
📜  通过翻转两个连续的位来检查是否所有位都可以相同

📅  最后修改于: 2021-04-29 06:53:25             🧑  作者: Mango

给定一个二进制字符串,任务是通过将任意两个连续位翻转任意次来查找字符串的所有数字是否相等(即0或1)。
例子:

Input: 01011
Output: YES
Explanation:
Flip 2nd and 3rd bit -> 00111, 
again flipping 1'st and 2'nd bit -> 11111

Input: 100011
Output: NO
Explanation:
No number of moves can ever 
equalize all elements of the array.

方法:
经过仔细观察,第i个和第j个位的切换可以通过从第i个位切换来完成,例如(i,i + 1),(i + 1,i + 2)…。 (j-1,j)在这里,每个位都切换两次(如果位被切换两次,则返回其初始值),除了i和j之外,最终第i个和第j个位切换。因此,可以说只有当1和0的计数都为奇数时,才不可能使二进制字符串的所有数字都相等。
下面是上述方法的实现:

C++
// C++ program for the
// above approach
 
#include 
using namespace std;
 
// Function to check if
// Binary string can be
// made equal
string canMake(string& s)
{
 
    int o = 0, z = 0;
 
    // Counting occurence of
    // zero and one in binary
    // string
    for (int i = 0; i < s.size(); i++) {
        if (s[i] - '0' == 1)
            o++;
        else
            z++;
    }
 
    // From above observation
    if (o % 2 == 1 && z % 2 == 1)
        return "NO";
    else
        return "YES";
}
 
// Driver code
int main()
{
 
    string s = "01011";
    cout << canMake(s) << '\n';
    return 0;
}


Java
// Java program for the above approach
class GFG
{
     
    // Function to check if
    // Binary string can be
    // made equal
    static String canMake(String s)
    {
     
        int o = 0, z = 0;
     
        // Counting occurence of
        // zero and one in binary
        // string
        for (int i = 0; i < s.length(); i++)
        {
            if (s.charAt(i) - '0' == 1)
                o++;
            else
                z++;
        }
     
        // From above observation
        if (o % 2 == 1 && z % 2 == 1)
            return "NO";
        else
            return "YES";
    }
     
    // Driver code
    public static void main (String[] args)
    {
     
        String s = "01011";
        System.out.println(canMake(s)) ;
         
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 program for the above approach
 
# Function to check if
# Binary string can be
# made equal
def canMake(s) :
 
    o = 0; z = 0;
 
    # Counting occurence of
    # zero and one in binary
    # string
    for i in range(len(s)) :
        if (ord(s[i]) - ord('0') == 1) :
            o += 1;
        else :
            z += 1;
 
    # From above observation
    if (o % 2 == 1 and z % 2 == 1) :
        return "NO";
    else :
        return "YES";
 
# Driver code
if __name__ == "__main__" :
 
    s = "01011";
    print(canMake(s));
 
# This code is contributed by AnkitRai01


C#
// C# program for the above approach
using System;
 
class GFG
{
     
    // Function to check if
    // Binary string can be
    // made equal
    static string canMake(string s)
    {
     
        int o = 0, z = 0;
     
        // Counting occurence of
        // zero and one in binary
        // string
        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] - '0' == 1)
                o++;
            else
                z++;
        }
     
        // From above observation
        if (o % 2 == 1 && z % 2 == 1)
            return "NO";
        else
            return "YES";
    }
     
    // Driver code
    public static void Main()
    {
        string s = "01011";
        Console.WriteLine(canMake(s)) ;
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
YES

时间复杂度: O(n),其中n是给定二进制数的长度