'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });
Given two numbers. Our task is to check whether they are anagrams of each other or not in binary representation. We can solve this problem quickly in python using Counter (iterable) method and Dictionary Comparison.
Examples
Input: a = 8, b = 16 Output : YesBinary representations of both numbers have same 0s and 1s.
Algorithm
Step 1 : Given two numbers.Step 2 : Convert both number into its binary using bin() function and remove first two characters because of bin().Step 3 : Since binary representation of both numbers could differ in length so we will append zeroes in start of shorter string to make both string of equal length.Step 4 : 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. Binary representations into dictionary.
Example Code
# function to Check if binary representations # of two numbers are anagram from collections import Counter def anagramoftwonumber(p1,p2): # convert numbers into in binary # and remove first two characters of # output string because bin function #'0b' as prefix in output string bno1 = bin(p1)[2:] bno2 = bin(p2)[2:] # append zeros in shorter string zeros = abs(len(bno1)-len(bno2)) if (len(bno1)>len(bno2)): bno2 = zeros * '0' + bno2 else: bno1 = zeros * '0' + bno1 # convert binary representations # into dictionary dict1 = Counter(bno1) dict2 = Counter(bno2) # compare both dictionaries if dict1 == dict2: print(p1, p2 ,"are two anagram number") else: print(p1 , p2 ,"are not anagram number") # Driver program if __name__ == "__main__": n1 = int(input("Enter First number ::>")) n2 = int(input("Enter Second number ::>")) anagramoftwonumber(n1,n2)
Output
Enter First number ::>8Enter Second number ::>168 16 are two anagram numberEnter First number ::>3Enter Second number ::>23 2 are not anagram number
Updated on: 30-Jul-2019
524 Views
Related Articles
- Java program to check if binary representations of two numbers are anagram
- Python program to check if binary representation is palindrome?
- Java Program to Check if two strings are anagram
- Golang Program to Check if two Strings are Anagram
- Haskell Program to Check if two strings are anagram
- Python Program to Check If Two Numbers are Amicable Numbers
- C# program to check if binary representation is palindrome
- Java program to check if binary representation is palindrome
- Golang Program to check if two numbers are Amicable Numbers
- Check if binary representation of a number is palindrome in Python
- Check if two strings are anagram of each other using C++
- Check whether two strings are anagram of each other in Python
- Python Program to Check if two arrays are equal
- Golang Program to check if the binary representation of a number is palindrome or not
- Check if sum of divisors of two numbers are same in Python
Advertisem*nts
';adpushup.triggerAd(ad_id); });