Goal:
Given an array of strings, create abbreviated versions of each string.
Specification:
For this challenge, an abbreviation is the first N characters of a string. For the string abc
: a
, ab
, and abc
are all valid abbreviations, while bc
, and ac
are not.
Given an array of strings, we want to find the shortest set of abbreviations, such that given the input and any abbreviation, you could determine which item of the input that the abbreviation was referring to.
Example:
Input: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
We work our way through the strings starting with the first one.
Monday is only the item string with an
M
, so the shortest possible abbreviation isM
.Tuesday starts with
T
, but so does Thursday. This means that we try the stringTU
. Since no other strings start with that, we useTU
.Wednesday is
W
, Thursday isTh
, and Friday isF
.
More Examples:
Input: "one,two,three,four,five,six,seven"
Output: "o,tw,th,fo,fi,si,se"
Input: "red,orange,yellow,green,blue,purple"
Output: "r,o,y,g,b,p"
Input: "a,ab,abc"
Output: Not valid! No abbreviation for `a` that doesn't apply to the other items.
Notes:
You make input and output in any reasonable way.
You can assume that input will always be a valid array of strings.
You can assume that there will always be a solution, unlike in the last test case.
Strings will only consist of printable ASCII (or the printable characters in your encoding)
This is code golf, so fewest bytes win!
U
for Tuesday, but a lowercaseh
for Thursday. \$\endgroup\$