📌  相关文章
📜  交换s1的两位以使s1和s2的按位或改变的方式的数目

📅  最后修改于: 2021-05-25 09:30:26             🧑  作者: Mango

给定两个二进制数字字符串s1   s2   的长度N   。在s1中找到交换两位的方式数目(仅s1而不是s2),以便更改这两个数字s1s2的按位或。
注意:两个字符串的长度必须相等,如果长度不同,则可以采用前导零。
例子:

Input: s1 = "01011", s2 = "11001"
Output: 4
Explanation:
You can swap the bit of s1 at indexed:
(1, 4), (2, 3), (3, 4) and (3, 5)
there are 4 ways possible.

Input: s1 = "011000", s2 = "010011"
Output: 6

方法:将变量结果初始化为零,该结果存储交换s1的位的方式数目,以使s1和s2的按位OR改变。初始化四个变量说a   b   c   , 和d   为零。
从左侧遍历两个字符串,并检查以下条件以增加字符串的值
上面声明的变量:

  • 如果s1和s2的当前位均为零,则递增c。
  • 如果s2的当前位为零且s1为1,则递增d。
  • 如果s2的当前位为1并且s1为零,则递增a。
  • 如果s1和s2的当前位均为1,则递增b。

(a * d)+(b * c)+(c * d)更新结果并返回结果。
下面是上述方法的实现:

C++
// C++ program to find no of ways
// to swap bits of s1 so that
// bitwise OR of s1 and s2 changes
#include 
using namespace std;
 
// Function to find number of ways
int countWays(string s1, string s2, int n)
{
 
    int a, b, c, d;
    a = b = c = d = 0;
 
    // intialise result that store
    // No. of swaps required
    int result = 0;
 
    // Traverse both strings and check
    // the bits as explained
    for (int i = 0; i < n; i++) {
        if (s2[i] == '0') {
            if (s1[i] == '0') {
                c++;
            }
            else {
                d++;
            }
        }
        else {
            if (s1[i] == '0') {
                a++;
            }
            else {
                b++;
            }
        }
    }
 
    // calculate result
    result = a * d + b * c + c * d;
 
    return result;
}
 
// Driver code
int main()
{
    int n = 5;
    string s1 = "01011";
    string s2 = "11001";
 
    cout << countWays(s1, s2, n);
 
    return 0;
}


Java
// Java program to find no of ways
// to swap bits of s1 so that
// bitwise OR of s1 and s2 changes
import java.io.*;
 
class GFG {
   
 
 
// Function to find number of ways
static int countWays(String s1, String s2, int n)
{
 
    int a, b, c, d;
    a = b = c = d = 0;
 
    // intialise result that store
    // No. of swaps required
    int result = 0;
 
    // Traverse both strings and check
    // the bits as explained
    for (int i = 0; i < n; i++) {
        if (s2.charAt(i) == '0') {
            if (s1.charAt(i) == '0') {
                c++;
            }
            else {
                d++;
            }
        }
        else {
            if (s1.charAt(i) == '0') {
                a++;
            }
            else {
                b++;
            }
        }
    }
 
    // calculate result
    result = a * d + b * c + c * d;
 
    return result;
}
 
// Driver code
 
    public static void main (String[] args) {
        int n = 5;
    String s1 = "01011";
    String s2 = "11001";
 
    System.out.println(countWays(s1, s2, n));
    }
}
// This code is contributed by shs..


Python3
# Python 3 program to find no of ways
# to swap bits of s1 so that
# bitwise OR of s1 and s2 changes
 
# Function to find number of ways
def countWays(s1, s2, n):
    a = b = c = d = 0
 
    # intialise result that store
    # No. of swaps required
    result = 0;
 
    # Traverse both strings and check
    # the bits as explained
    for i in range(0, n, 1):
        if (s2[i] == '0'):
            if (s1[i] == '0'):
                c += 1;
             
            else:
                d += 1
        else:
            if (s1[i] == '0'):
                a += 1
             
            else:
                b += 1
         
    # calculate result
    result = a * d + b * c + c * d
 
    return result
 
# Driver code
if __name__ == '__main__':
    n = 5
    s1 = "01011"
    s2 = "11001"
 
    print(countWays(s1, s2, n))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find no of ways
// to swap bits of s1 so that
// bitwise OR of s1 and s2 changes
using System;
 
class GFG {
 
 
 
// Function to find number of ways
static int countWays(string s1, string s2, int n)
{
 
    int a, b, c, d;
    a = b = c = d = 0;
 
    // intialise result that store
    // No. of swaps required
    int result = 0;
 
    // Traverse both strings and check
    // the bits as explained
    for (int i = 0; i < n; i++) {
        if (s2[i] == '0') {
            if (s1[i] == '0') {
                c++;
            }
            else {
                d++;
            }
        }
        else {
            if (s1[i] == '0') {
                a++;
            }
            else {
                b++;
            }
        }
    }
 
    // calculate result
    result = a * d + b * c + c * d;
 
    return result;
}
 
// Driver code
 
    public static void Main () {
        int n = 5;
    string s1 = "01011";
    string s2 = "11001";
 
    Console.WriteLine(countWays(s1, s2, n));
    }
}
// This code is contributed by shs..


PHP


Javascript


输出:
4

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。