I am thinking about following problem, but am not able to find answer.
In how many ways can a number be formed only using $3$ digits $(0, 1, 2)$ given following constraints with $K$ and $D$:-
1) Each digit appears exactly K times. The total length of the number is $3*K$.
2) At any given position (staring from left), the number of $0$'s must be at most $D$ more than the number of $1$'s and number of $1$'s must be at most D more than number of $2$'s.
i.e. for $K =3$ and $D =1$ followings are some valid numbers.
$012012012 \ \ \ \ \ \ 010212012$
The number $001201212$ will be invalid as there are $2$ two's before the first $1$. Similarly 010122012 is invalid as there are two $1$'s are there before first $2$.
The number $001201212$ will be valid if $D=2$.
So far I have only found out the total number of numbers which can be formed is $\binom{3K}{K}\binom{2K}{K}$