You are given a number N. A number is called beautiful if, for every digit x in the number, there are x occurrences of it in the number.
Example:
1 is beautiful because 1 has 1 occurrence. 3133 is beautiful because 1 has 1 occurrence and 3 has 3 occurrences. 224 is not beautiful because 4 does not have 4 occurrences. Find the smallest beautiful number which is greater than N
Example: N=299 Output: 333
def solve(N):
n=list(str(N))
d={}
for i in n:
if i in d:
d[i]+=1
else:
d[i]=1
for i in d:
if int(i)!=d[i]:
return False
return True
def beautifulNumber (N):
i=1
while(True):
if solve(N+i):
return(N+i)
i=i+1
N = int(input())
print(beautifulNumber(N))
constraints: 1<N<10^12
Code takes a lot of time for huge numbers like 10^10. need it optimized.