# Pattern Matching and Permutations

I am working with some math students on some problems involving permutations. My Mathematica knowledge is limited and I'm hoping to get some help. Two sample problems: a)how many ways can the letters of the word UNITED be arranged if each arrangement begins with two consonants? b)how many ways can the letters of the word UNITED be arranged if each arrangement begins with exactly two consonants?

The math is "easy". My motivation is to be able to learn something about pattern matching and also, to be able to actually show my students that the methods actually work!

Any help is appreciated, and I especially value clarity over brevity!

This is an example of solving such a task. It is divided into separate steps, which I believe are – due to quite descriptive naming conventions in Mathematica – self-explanatory. The only two mysterious parts to someone coming from another programming language are probably __, a shorthand for BlankSequence, which is used to represent an arbitrary number of characters, and |, a shorthand for Alternatives. As a general tutorial, you can guide them to An Elementary Introduction to the Wolfram Language.

(* Define a word *)
word = "UNITED";

(* Extract a list of characters *)
characters = Characters[word];

(* Calculate all permutations *)
permutations = Permutations[characters];

(* Count the number of all permutations *)
Length[permutations]
(* 720 *)

(* Define a pattern for a vowel *)
vowel = "A" | "E" | "I" | "O" | "U";

(* Define a pattern for a consonant *)
consonant = Except[vowel];

(* Select permutations which begin with two consonants *)
problemA = Cases[permutations, {consonant, consonant, __}];
Length[problemA]
(* 144 *)

(* Select permutations which begin with two consonants *)
problemB = Cases[permutations, {consonant, consonant, vowel, __}];
Length[problemB]
(* 108 *)

• Amazing, thanks for the clarity of your response and all your helpful comments! I can use this for many examples in the course I am helping with. Commented Apr 24 at 16:57