Given two numbers you are required to check whether they are anagrams of each other or not in binary representation.
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 Comparision. 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.
- Python sorted() to check if two strings are anagram or not
- Python | Check if key has Non-None value in dictionary
- Python | Check if all values are 0 in dictionary
- Python | Check whether given key already exists in a dictionary
- Python | Check if one dictionary is subset of other
- Python | Check if dictionary is empty
- Python | Check for None values in given dictionary
- Python dictionary, set and counter to check if frequencies can become same
- Python | Check if given multiple keys exist in a dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Pretty Print a dictionary with dictionary value
- Python | Convert string dictionary to dictionary
- Python | Check if there are K consecutive 1's in a binary number
- Python | Check if binary representation is palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.