Approach

Written by Jasper Di Francesco from CSESoc Education

My solution can be broken down into 3 sections.

  1. Go through the word, find if the vowels are in the odd/even position
  2. Go through the word again, upon reaching a hyphen add in the highest scoring available letter.
  3. Find the total value of the word.

Part 1 - finding the positions of the vowels

This is an important step as it defines which spots in the target word can contain vowels and which can contain consonants.

We will loop through the word until we come across a character which is not a hyphen. Then we check if it is a vowel or not.

int vowelIndex = 0;
// Find index of vowels
for (int i = 0; i < n; i++)
{
if (word[i] != '-')
{
if (isVowel(word[i]))
{
vowelIndex = i % 2;
}
else
{
vowelIndex = (i + 1) % 2;
}
}
}

Here the vowelIndex can be a 1 or a 0. If it's a 1 then vowels should go in the odd positions, if it's 0 then they should go in the even positions.

Part 2 - Adding in letters

An important point to pick up on is that the letter bank is in reverse alphabetical order. So once we encounter a hyphen in the target word we want to add in the next vowel/consonant.

If the letter is not a hyphen then we do not need to assign it anything, so we continue:

if (word[i] != '-')
{
continue;
}

After checking that the current position in word is a hyphen, we define a variable searchInd, which is the index into our counts array. If searchInd is 0 then we are expecting a consonant so go into word[i]. If it is a 1 then we expect a vowel in word[i].

We then search through the letter bank from counts[searchInd] onward until we reach one of the following:

  • a consonant if we are looking for a consonant,
  • a vowel if we are looking for a vowel, or
  • the end of the letter bank.

If we find a letter, we insert it into the word and increment counts[searchInd].

If we finish the searching loop and find that word[i] is still a hyphen, then there was no suitable letter to put into word[i] so we exit with an error message.

Part 3 - Total of word

This is the simplest step, we iterate through the target word and add to a total w[i] - 'a' + 1 which finds the alphabetical value of a letter.