VOOZH about

URL: https://www.geeksforgeeks.org/dsa/xor-of-two-binary-strings/

⇱ XOR of two Binary Strings - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

XOR of two Binary Strings

Last Updated : 3 Nov, 2023

Given two binary strings A and B of equal lengths, the task is to print a string that is the XOR of Binary Strings A and B.

Examples:

Input: A = "0001", B = "0010" 
Output: 0011

Input: A = "1010", B = "0101" 
Output: 1111 

Approach: The idea is to iterate over both the string character by character and if the character is mismatched then add "1" as the character in the answer string otherwise add "0" to the answer string to generate the XOR string.
👁 Image

Below is the implementation of the above approach:


Output
0111

Time Complexity: O(N)
Auxiliary Space: O(N)

method2:performs XOR operation bit by bit to generate the XOR result string.

Approach: We take  two binary strings A and B as input which  are of equal length, and apply for loop and performs XOR operation bit by bit to generate the XOR result. Then we print the XOR result string.

Below is the implementation of the above approach:


Output
0111

Time Complexity: O(N)
Auxiliary Space: O(N)

Comment
Article Tags:
Article Tags: