# Check if frequency of each digit is less than the digit in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

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)
• return True

## Example

Let us see the following implementation to get better understanding −

Live Demo

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
Updated on 18-Jan-2021 12:41:28