# B4B solution
from itertools import permutations
from bisect import bisect

PERMS = [int(''.join(p)) for p in permutations('0123456789') if p[0] != '0']

def next_happy_number(number):
    return PERMS[bisect(PERMS,number)]
    
def _next_happy_number(n):

    ds = [int(c) for c in str(n)]
    m = len(ds)
    i, uniq = 0, set()
    # Find maximum set of uniq digits from left to right
    while i + 1 < m:
        uniq.add(ds[i])        
        if len(uniq) != i + 1:
            break
        i += 1
    
    while i > 0:
        # Find a candidate for digit at position i + 1
        cand = set(range(10)) - set(ds[:i]) - set(range(ds[i] + 1))
        if len(cand) == 0:
            # no candidate, go left
            i -= 1
        else:
            # good candidate, add smallest digits
            d = min(cand)
            break
    if i == 0:
        xs = [ds[i] + 1] + sorted(set(range(10)) - set([ds[i] + 1]))[:m - 1]
        return int(''.join(map(str, xs)))

    
    fst = ds[:i] + [d]
    xs = fst + sorted(set(range(10)) - set(fst))[:m - i - 1]

    return int(''.join(map(str, xs)))

        

print(next_happy_number(1023456798))

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: