Ad

Objective: given 1 <= K <= 26, 0 <= N count the number of different strings of length N that contain exactly K different characters from the alphabet.

Ideas: for small values of N it should be bruteforce-able.
For large values of N?

Clarifications:
-> "The alphabet" is the 26 lowercase letters of the English alphabet

alphabet = 'abcdefghijklmnopqrstuvwxyz'

import itertools

def counter(N, K):
    return counterNaive(N, K)

def counterNaive(N, K):
#    print(f"counterNaive({K}, {N})")
    allCombs = [x for x in itertools.product(alphabet, repeat=N)]
#    print(f"All combinations of length {N}: 26^N = {26**N}")
    return sum( len(set(x)) == K for x in allCombs )
    
def counterSmart(N, K):
    # Idea:
    # Find all ways of splitting N in K parts
    # For each such way, once we know the lenght of the parts K1, ..., Kn
    # (And they must be such that K1+...+Kn = N)
        # Calculate the possible combinations: they are N!/(N-K)! options for the choice of K letters
        # divided by the product of:
            # L! where L = count(Kn == j) for j in range(N)
            # to compensate the fact that if letters 'a' and 'b' appear in the same amount,
            # then doing (a, b) = (b, a) does not give a different sequenec
        # times the permutations of N elements with K1, ..., Kn repetitions
    # Then add these up
    pass