The OP's solution didn't deal with letter counts at all; my first expression was to show him a similar solution that was more succinct to illustrate the principle at work. My second solution deals with multiplicity properly.
https://gist.github.com/rilindo/6576017
New toy to play with. :)