This question is based on a question I asked in Spanish language. Yes, I asked for an algorithm in Spanish Language. :)
In Spain, current license plates have this pattern:
1234 XYZ
where XYZ are three consonants taken from the full set of Spanish consonants (except the 'Ñ', I think).
Sometimes, when traveling with my wife, we use to play a game. When we see a license plate, we take its three consonants and try to form a word that contains those three consonants, appearing in the same order as in the license plate. Examples (in Spanish):
BCD
BoCaDo (valid)
CaBezaDa (not valid)
FTL
FaTaL (valid)
FLeTar (not valid)
FTR
FleTaR (valid, wins)
caFeTeRa (valid, loses)
The winner is the one who uses the least number of characters, as you can see in the last example.
The challenge
Write the shortest program or function that receives a list of words and a set of three consonants and finds the shortest word in the list that contains the three consonants in the same order. For the purposes of this game, case does not matter.
- The input for the word list (first parameter) will be an array of your language
string
type. The second parameter (the three consonants) will be anotherstring
. If it's better for your language, consider thestring
with the three consonants the last item of the whole list of parameters. The output will be anotherstring
. - The words in the word list won't be invented or infinite words, they will word that appear in any standard dictionary. If you need a limit, suppose no word in the word list will be longer than 50 characters.
- If there are several words with the same lenght that could be the valid answer, you can return any one of them. Just make sure you return just one word, or an empty string if no words match the pattern of three consonants.
- You can repeat consonants in the group, so valid inputs for the three consonants are both
FLR
andGGG
. - The Spanish consonants are exactly the same as English, with the addition of the "Ñ". The vowels are the same with the adition of the stressed vowels: "áéíóúü". There won't be any other kind of marks such as "-" or "'".
- You can suppose the case will always be the same in both the word list and the three consonants.
If you want to test your algorithm with a real collection of Spanish words, you can download a file (15.9 MB) from Dropbox with more than a million words.
Test cases
Input: 'psr', {'hola' 'repasar' 'pasarais' 'de' 'caída' 'pequeñísimo' 'agüeros'}
Output: 'repasar'
Input: 'dsd', {'dedos' 'deseado' 'desde' 'sedado'}
Output: 'desde'
Input: 'hst', {'hastío' 'chest'}
Output: 'chest'
This is code-golf, so may the shortest program that helps me to always beat my wife wins! :)