Consider the set of 26 alphabet and 10 digits.
Write a function which returns the numbers of passwords of length N containing at-least L lowercase letters, at-least U uppercase letters and at-least D digits.
Function Signature int cntPass(int N,int L,int U,int D)
My approach:
I was trying to use recursion to solve it but I think its wrong.My (wrong) recurrence was as follows:
f(N,L,U,D)=f(N,L-1,U,D)+f(N,L,U-1,D)+f(N,L,U,D-1) [with the necessary base conditions but it didnt work].
I am looking for a better approach or a different logic to solve this problem.
Thanks.
This is just a simple combinatorics problem. The result is NCL * N-LCU * N-L-UCD * 26L * 26U * 10D * 62N – U – L – D, which can be simplified a bit to 26L + U * 10D * 62N – U – L – D * N! / ( U! * L! * D! * (N – L – U – D)! ).
We choose L places for the lower case characters among N places. Then choose U places for upper case letters among the rest N – L places. And choose D places for digits among the rest N – L – U places. The rest are anything goes.
L lower case letters have 26 choices each. Same for U upper case letters. D digits have 10 choices each. For the rest (N – L – U – D), we can use any of 26 + 26 + 10 characters for each of them.