Suppose we have two numbers x and y we have to check whether the binary representation of x and y are anagram of each other or not.
So, if the input is like x = 9 y = 12, then the output will be True as binary representation of 9 is 1001 and binary representation of 12 is 1100, so these two are anagram of each other.
To solve this, we will follow these steps −
- if number of 1s in x and y are same, then
- return True
- return False
Let us see the following implementation to get better understanding −
Example
def set_bit_count(num) : cnt = 0 while num: cnt += num & 1 num >>= 1 return cnt def solve(x, y) : if set_bit_count(x) == set_bit_count(y): return True return False x = 9 y = 12 print(solve(x, y))
Input
9, 12
Output
True