Given two numbers you are required to check whether they are anagrams of each other or not in binary representation.
Examples:
Input : a = 8, b = 4 Output : Yes Binary representations of both numbers have same 0s and 1s. Input : a = 4, b = 5 Output : No
We have existing solution for this problem please refer Check if binary representations of two numbers are anagram link. We can solve this problem quickly in python using Counter(iterable) method and Dictionary Comparison. Approach is simple,
- Convert both number into it’s binary using bin() function.
- Since binary representation of both numbers could differ in length so we will append zeros in start of shorter string to make both string of equal length. ie.; append zeros = abs(len(bin1)-len(bin2)).
- Convert both output string containing 0 and 1 returned by bin function into dictionary using Counter() function, having 0 and 1 keys and their count as value. Compare both dictionaries, if value of 0’s and 1’s in both dictionaries are equal then binary representations of two numbers are anagram otherwise not.
# function to Check if binary representations # of two numbers are anagram from collections import Counter def checkAnagram(num1,num2): # convert numbers into in binary # and remove first two characters of # output string because bin function # '0b' as prefix in output string bin1 = bin(num1)[2:] bin2 = bin(num2)[2:] # append zeros in shorter string zeros = abs(len(bin1)-len(bin2)) if (len(bin1)>len(bin2)): bin2 = zeros * '0' + bin2 else: bin1 = zeros * '0' + bin1 # convert binary representations # into dictionary dict1 = Counter(bin1) dict2 = Counter(bin2) # compare both dictionaries if dict1 == dict2: print('Yes') else: print('No') # Driver program if __name__ == "__main__": num1 = 8 num2 = 4 checkAnagram(num1,num2) |
Output:
Yes
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python sorted() to check if two strings are anagram or not
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Visualizing representations of Outputs/Activations of each CNN layer
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Regular Dictionary vs Ordered Dictionary in Python
- Python | Dictionary initialization with common dictionary
- Python - Update dictionary with other dictionary
- Python - Filter dictionary values in heterogenous dictionary
- Python - Convert Dictionary Value list to Dictionary List
- Python - Replace dictionary value from other dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python program to update a dictionary with the values from a dictionary list
- Anagram checking in Python using collections.Counter()
- Python Counter to find the size of largest subset of anagram words
- Python - Binary operation on specific keys in Dictionary List
- Python | Convert two lists into a dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

