Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to check if binary representation of two numbers are anagram.
The binary numbers are the basis of how data is stored, processed and transferred. Every integer is internally represented by using a sequence of bits (combination of 0s and 1s).
In this article, we will learn how to check if binary representation of two numbers are anagram. Two numbers are said to be binary anagrams if their binary representations contain the same number of 1s and 0s. For example, the number 12 in binary is 1100 and the number 10 in binary is 1010, both have two 0s and two 1s, making them binary anagrams.
Using collections.Counter() Class
The collections module in Python provides specialized container datatypes. Counter is one among them that counts the frequency of characters in a string.
Counter is a subclass of the dictionary that is designed to count hashable objects. It takes an iterable or mapping and returns a dictionary-like object, where elements are stored as keys and their counts as values.
Syntax
Following is the syntax of collections.Counter() class ?
from collections import Counter Counter(iterable)
Example 1
Let's check whether numbers 12 and 10 are binary anagrams ?
from collections import Counter
def check_binary_anagram(a, b):
x1 = bin(a)[2:]
x2 = bin(b)[2:]
print(f"Binary of {a}: {x1}")
print(f"Binary of {b}: {x2}")
return Counter(x1) == Counter(x2)
a = 12
b = 10
result = check_binary_anagram(a, b)
print("Result:", result)
The output of the above program is ?
Binary of 12: 1100 Binary of 10: 1010 Result: True
Example 2
Consider another scenario, where we check whether 10 and 6 are binary anagrams ?
from collections import Counter
def check_binary_anagram(a, b):
x1 = bin(a)[2:]
x2 = bin(b)[2:]
print(f"Binary of {a}: {x1}")
print(f"Binary of {b}: {x2}")
return Counter(x1) == Counter(x2)
a = 10
b = 6
result = check_binary_anagram(a, b)
print("Result:", result)
The output of the above program is ?
Binary of 10: 1010 Binary of 6: 110 Result: False
Using Manual Count Comparison
We can also check binary anagrams by manually counting the number of 1s and 0s in each binary representation ?
def check_binary_anagram_manual(a, b):
x1 = bin(a)[2:]
x2 = bin(b)[2:]
print(f"Binary of {a}: {x1}")
print(f"Binary of {b}: {x2}")
# Count 1s and 0s in both numbers
count1_ones = x1.count('1')
count1_zeros = x1.count('0')
count2_ones = x2.count('1')
count2_zeros = x2.count('0')
print(f"Number {a} - 1s: {count1_ones}, 0s: {count1_zeros}")
print(f"Number {b} - 1s: {count2_ones}, 0s: {count2_zeros}")
return count1_ones == count2_ones and count1_zeros == count2_zeros
a = 12
b = 9 # Binary: 1001
result = check_binary_anagram_manual(a, b)
print("Result:", result)
The output of the above program is ?
Binary of 12: 1100 Binary of 9: 1001 Number 12 - 1s: 2, 0s: 2 Number 9 - 1s: 2, 0s: 2 Result: True
Comparison
| Method | Pros | Cons |
|---|---|---|
| Counter() | Concise, handles any character frequency | Requires imports |
| Manual Count | No imports needed, explicit logic | More verbose code |
Conclusion
Both methods effectively check if two numbers are binary anagrams by comparing the count of 1s and 0s in their binary representations. The Counter() approach is more concise, while manual counting provides explicit control over the comparison logic.
