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 dictionary is empty
- Python | Check whether given key already exists in a dictionary
- 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 | 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
- Python | Check whether string contains only numbers or not
- Anagram checking in Python using collections.Counter()
- Python | Check if list contains consecutive numbers
- Python Counter to find the size of largest subset of anagram words