# The Spain license plates game

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
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 another string. If it's better for your language, consider the string with the three consonants the last item of the whole list of parameters. The output will be another string.
• 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 and GGG.
• 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'

Output: 'desde'

Input: 'hst', {'hastío' 'chest'}
Output: 'chest'

This is , so may the shortest program that helps me to always beat my wife wins! :)

• How long are the words in the word list guaranteed to be? – Neil Jun 17 '17 at 14:25
• In actual license plates, letter Q is not allowed either; and W is, although not a proper Spanish letter – Luis Mendo Jun 17 '17 at 14:31
• May we assume the words in the list and the three letters will be all in one case? – Jonathan Allan Jun 17 '17 at 14:39
• @LuisMendo W has been a Spanish letter since 1969. – walen Jun 19 '17 at 7:22
• @walen That's why I said "proper" :-) It exists in Spanish, but is feels foreign – Luis Mendo Jun 19 '17 at 8:47

# 05AB1E, 10 8 bytes

Saved 2 bytes thanks to Leo

ʒæså}éR

Try it online!

Explanation

ʒ         # filter list, keep only members for which the following is true
så      # input is in the
æ        # powerset of the current word
}     # end filter
é    # sort by length
R   # reverse
# push separately (shortest on top)

I would have used head at the end saving a byte but that would output an empty list if there isn't a match.

• 3ù #keep only those of length 3 why do you need this? – Leo Jun 17 '17 at 15:44
• @Leo: I don't, that was silly of me. Thanks :) – Emigna Jun 17 '17 at 15:45

# MATL, 30 29 bytes

xtog!s2$S"1G!'.*'Yc!@gwXXn?@. Try it online! ### Explanation x % Implicitly take first input (string with three letters). Delete. % Gets copied into clipboard G, level 1 t % Implicitly take second input (cell array of strings defining the % words). Duplicate o % Convert to numeric array of code points. This gives a matrix where % each string is on a row, right-padded with zeros g % Convert to logical: nonzeros become 1 !s % Sum of each row. This gives the length of each word 2$S       % Two-input sort: this sorts the array of strings according to their
% lengths in increasing order
"         % For each word in the sorted array
1G      %   Push first input, say 'xyz'
!       %   Transpose into a column vector of chars
'.*'Yc  %   Concatenate this string on each row
!       %   Transpose. This gives a char array which, when linearized in
%   column-major order, corresponds to 'x.*y.*z.*'
@g      %   Push corrent word
w       %   Swap
XX      %   Regexp matching. Gives a cell array with substrings that match
%   the pattern 'x.*y.*z.*'
n       %   Number of matchings
?       %   If non-zero
@     %     Push cell array with current word, to be displayed as output
.     %     Break loop
%   Implicit end (if)
% Implicit end (for)
% Implicitly display stack

# PHP, 111 bytes

CREDIT to @nimi

• You can replace the rightmost map and the filter with a list comprehension. As you already have Data.List in scope, you can use sortOn length and pick the head to find the element with minimal length. Finally, make y an infix function. All this makes f and k superfluous: l#w=sortOn length[p|p<-w,isInfixOf l$filter(eleml)p]!!0. – nimi Jun 17 '17 at 16:50 • you're right! I just started golfing! Thanks! – Davide Spataro Jun 17 '17 at 17:23 • One more: if you switch the import to Data.Lists, you can use argmin instead of sortOnand save the !!0: l#w=argmin length[...]. Data.Lists has many nice functions – nimi Jun 17 '17 at 17:42 ## Perl, 53 bytes 48 bytes code + 5 for -paF.$"=".*";($_)=sort{$a=~y///c-length$b}grep/@F/,<> This takes advantage of the fact that lists interpolated into the m// operator utilise the$" variable which changes the initial input string from psr to p.*s.*r which is then matched for each additional word and is sorted on length.

Try it online!

• If I insert "adsd" into your list, your program fails to find it. The first character to find does not need to be the first in the word. – Charlie Jun 17 '17 at 16:05
• @CarlosAlejo The input needs a trailing newline, works ok then: Try it online!. That did catch me off guard though, as the <<< operator adds that in for me at command line! – Dom Hastings Jun 17 '17 at 16:39

## JavaScript (ES6), 7775 72 bytes

Takes the 3 consonants c and the list of words l in currying syntax (c)(l). Both inputs are expected in the same case.

c=>l=>l.map(w=>x=!w.match([...c].join.*)||!x[w.length]&&x?x:w,x='')&&x

### Test cases

let f =

c=>l=>l.map(w=>x=!w.match([...c].join.*)||!x[w.length]&&x?x:w,x='')&&x

console.log(f('psr')(['hola', 'repasar', 'pasarais', 'de', 'caída', 'pequeñísimo', 'agüeros'])) // 'repasar'

• c=>l=>l.sort((a,b)=>a[b.length]&&1).find(w=>w.match(c.split.join.*)) for 72, I think – LarsW Jun 17 '17 at 14:49
• @LarsW Indeed, thanks! However I've chosen another approach to comply with the new rule: or an empty string if no words match the pattern of three consonants. – Arnauld Jun 17 '17 at 14:54

### R, 101 bytes

First time golfing! I'm sure this can be condensed somehow

Takes the string x and a character vector y of possible inputs

w=pryr::f((b=y[sapply(gsub(paste('[^',x,']'),'',y),function(l)regexpr(x,l))>0])[which.min(nchar(b))])

Try it online!

Edit: My version was 135, thanks Scrooble for the -34!

• Welcome to PPCG! This looks like a snippet where the input is in hardcoded variables. Answers need to be either full programs or callable functions. You can have a look at this (or other R answers) for possible I/O methods. – Martin Ender Feb 28 '18 at 21:01

# Retina, 58 bytes

O#$^¶.+$.&
s^((.)(.)(.).*¶(?-s:(.*\2.*\3.*\4.*)))?.*