检查两个二进制字符串是否可以通过交换 0 之前出现的 1 来使其相等


在本文中,我们将讨论一个与字符串操作和 C++ 中二进制数相关的有趣问题。我们将要解决的问题是“检查两个二进制字符串是否可以通过交换 0 之前出现的 1 来使其相等”。这个问题是增强您对字符串、二进制数和算法思维理解的绝佳方法。

问题陈述

任务是确定两个二进制字符串是否可以通过交换字符串中 0 之前出现的 1 来使其相等。

C++ 解决方案方法

解决此问题的方法是跟踪两个字符串中 1 和 0 的数量。当且仅当两个字符串具有相同数量的 1 和 0 时,两个二进制字符串可以通过交换 0 之前的 1 来使其相等。

示例

以下是实现此解决方案的程序 -

#include <stdio.h>
#include <string.h>

int canBeMadeEqual(char *str1, char *str2) {
   int count1s_str1 = 0, count0s_str1 = 0;
   int count1s_str2 = 0, count0s_str2 = 0;

   // Count the number of '1's and '0's in str1
   for (int i = 0; str1[i] != '\0'; i++) {
      if (str1[i] == '1') {
         count1s_str1++;
      } else {
         count0s_str1++;
      }
   }

   // Count the number of '1's and '0's in str2
   for (int i = 0; str2[i] != '\0'; i++) {
      if (str2[i] == '1') {
         count1s_str2++;
      } else {
         count0s_str2++;
      }
   }

   // Compare the counts of '1's and '0's for both strings
   return count1s_str1 == count1s_str2 && count0s_str1 == count0s_str2;
}

int main() {
   char str1[] = "1100";
   char str2[] = "1010";
   int result = canBeMadeEqual(str1, str2);

   // Print the result based on whether the strings can be made equal or not
   printf(result ? "The binary strings can be made equal.\n" : "The binary strings cannot be made equal.\n");
   return 0;
}

输出

The binary strings can be made equal.
#include <iostream>
#include <string>
using namespace std;

bool canBeMadeEqual(string str1, string str2) {
   int count1s_str1 = 0, count0s_str1 = 0;
   int count1s_str2 = 0, count0s_str2 = 0;
   
   for (char c : str1) {
      if (c == '1') {
         count1s_str1++;
      } else {
         count0s_str1++;
      }
   }
   
   for (char c : str2) {
      if (c == '1') {
         count1s_str2++;
      } else {
         count0s_str2++;
      }
   }
   
   return count1s_str1 == count1s_str2 && count0s_str1 == count0s_str2;
}

int main() {
   string str1 = "1100";
   string str2 = "1010";
   bool result = canBeMadeEqual(str1, str2);
   cout << (result ? "The binary strings can be made equal." : "The binary strings cannot be made equal.") << endl;
   return 0;
}

输出

The binary strings can be made equal.
public class BinaryStringEquality {
   public static boolean canBeMadeEqual(String str1, String str2) {
      int count1s_str1 = 0, count0s_str1 = 0;
      int count1s_str2 = 0, count0s_str2 = 0;

      // Count the number of '1's and '0's in str1
      for (char c : str1.toCharArray()) {
         if (c == '1') {
            count1s_str1++;
         } else {
            count0s_str1++;
         }
      }

      // Count the number of '1's and '0's in str2
      for (char c : str2.toCharArray()) {
         if (c == '1') {
            count1s_str2++;
         } else {
            count0s_str2++;
         }
      }

      // Compare the counts of '1's and '0's for both strings
      return count1s_str1 == count1s_str2 && count0s_str1 == count0s_str2;
   }

   public static void main(String[] args) {
      String str1 = "1100";
      String str2 = "1010";
      boolean result = canBeMadeEqual(str1, str2);

      // Print the result based on whether the strings can be made equal or not
      System.out.println(result ? "The binary strings can be made equal." : "The binary strings cannot be made equal.");
   }
}

输出

The binary strings can be made equal.
def can_be_made_equal(str1, str2):
   count1s_str1 = str1.count('1')
   count0s_str1 = str1.count('0')
   count1s_str2 = str2.count('1')
   count0s_str2 = str2.count('0')
   
   # Compare the counts of '1's and '0's for both strings
   return count1s_str1 == count1s_str2 and count0s_str1 == count0s_str2

def main():
   str1 = "1100"
   str2 = "1010"
   result = can_be_made_equal(str1, str2)
   
   # Print the result based on whether the strings can be made equal or not
   print("The binary strings can be made equal." if result else "The binary strings cannot be made equal.")

if __name__ == "__main__":
   main()

输出

The binary strings can be made equal.

带测试用例的说明

让我们考虑二进制字符串“1100”和“1010”。

当我们将这些字符串传递给 canBeMadeEqual 函数时,它会计算两个字符串中 1 和 0 的数量。

在字符串“1100”中,有两个 1 和两个 0。在字符串“1010”中,也有两个 1 和两个 0。

由于两个字符串中 1 和 0 的数量相等,因此该函数返回 true,表明可以通过交换 0 之前的 1 来使字符串相等。

结论

此问题提供了一种绝佳的方式,可以应用字符串操作和二进制数的知识来解决 C++ 中的复杂问题。这是一个练习您的 C++ 编码技能并了解如何在字符串格式中处理二进制数的极佳问题。

更新于: 2023 年 10 月 16 日

301 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.