Given a string containing 'a' and 'b', Find all permutations starting with 'a'

70 Views Asked by At

Given a string contains only a and b where a comes M times and b comes N times in the string.

I need to compute all the permutation of the string starting with a. What is the most efficient way? Can we use properties of binary numbers ?

1

There are 1 best solutions below

0
On

We start with $a$ so we have $M-1$ times $a$ and $N$ times $b$.

$$\dfrac {(M+N-1)!}{(M-1)!(N!)} $$

Note: $(M+N-1)!$ is all possible permutation since permutation of $a's$ in itself does not change the "word" we divides $(M-1)!$ and with same reasoning we divides $N!$.