Suppose we have a number n, we have to check whether the occurrence of each digit of n is less than or equal to digit itself.
So, if the input is like n = 5162569, then the output will be True as the digits and frequencies are (5, 2), (1, 1), (6, 2) and (9, 1), for all the frequency is either small or equal to the digit value.
To solve this, we will follow these steps −
- for i in range 0 to 9, do
- temp := n, cnt := 0
- while temp is non-zero, do
- if temp mod 10 is same as i, then
- cnt := cnt + 1
- if cnt > i, then
- return False
- temp := quotient of (temp / 10)
- if temp mod 10 is same as i, then
- return True
Example
Let us see the following implementation to get better understanding −
def solve(n): for i in range(10): temp = n cnt = 0 while temp: if temp % 10 == i: cnt += 1 if cnt > i: return False temp //= 10 return True s = 5162569 print(solve(s))
Input
5162569
Output
True