3
$\begingroup$

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
$\endgroup$
0
6
$\begingroup$
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
$\endgroup$
3
  • $\begingroup$ It works perfectly! The last test (counting the consonants) is apparently optional. My bad! $\endgroup$ – Kim Jong Un Aug 17 '20 at 12:55
  • 1
    $\begingroup$ Ah sorry, I misunderstood. Yes, since you only have vowels and consonants. $\endgroup$ – Hausdorff Aug 17 '20 at 12:58
  • $\begingroup$ Thank you very much! $\endgroup$ – Kim Jong Un Aug 17 '20 at 13:03
3
$\begingroup$

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
$\endgroup$
1
  • $\begingroup$ Very good! Thank you! $\endgroup$ – Kim Jong Un Aug 19 '20 at 1:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.