# How to check whether a string contains a certain number of consonants and vowels?

I am trying to check the answer of the following problem programmatically. A manual calculation by hand must be possible but it is not my question.

Given a string "aeeiuchklpr" of length 11. The character "e" occurs twice. It is not a typo. How many 6-permutations of the given string are there? The constraints are given as follows

• the first character must be "h"
• the last character must be a vowel
• the number of consonants must be exactly 4
• the number of vowels must be exactly 2

# Attempt

I don't know how to check the last two requirement above. Here is my attempt

Select[Permutations[StringSplit["aeeiuchklpr", ""], {6}],
First[#] == "h" &&
(Last[#] == "a" || Last[#] == "e" || Last[#] == "i" ||
Last[#] == "u") &] // Length


VowelQ[s_String] := MatchQ[s, "a" | "e" | "i" | "o" | "u"];

Select[Permutations[StringSplit["aeeiuchklpr", ""], {6}],
First[#] == "h" && VowelQ[Last@#] && Count[VowelQ /@ #, True] == 2 &] // Length

3120

• It works perfectly! The last test (counting the consonants) is apparently optional. My bad! Commented Aug 17, 2020 at 12:55
• Ah sorry, I misunderstood. Yes, since you only have vowels and consonants. Commented Aug 17, 2020 at 12:58
• Thank you very much! Commented Aug 17, 2020 at 13:03

Slightly different implementation as a one-liner, using a restricted pattern:

Count[Permutations[
Characters["aeeiuchklpr"], {6}], {"h", m__,
vowels = ("a" | "e" | "i" | "o" | "u")} /; Count[{m}, vowels] == 1]


yields

3120

• Very good! Thank you! Commented Aug 19, 2020 at 1:22