This is a variant of Play the word chain and Building a long chain of words .
The input is a non-empty list of unique words at least 2 chars long made of characters in [a-z]. You need to output the length of the longest possible chain, where each subsequent word starts with the last letter of the previous word. You can start with any word on the list.
Another twist is that you are allowed to repeat any single word on the list. However, you can not repeat any two-word block. For example,
cat->tac->cat
is allowed, but cat->tac->cat->tac
is not, because you repeated a two-word block (cat->tac
). Also, you can not use the same word twice in a row (e.g. eye->eye
).
Examples:
cat dog tree egg
=> 3 (cat->tree->egg)new men ten whim
=> 5 (ten->new->whim->men->new)truth fret heart his
=> 5 (fret->truth->heart->truth->his)we were stew early yew easy
=> 9 (stew->were->early->yew->were->easy->yew->we->easy)tac cat tac cot tac can
=> 6 (tac->cat->tac->cot->tac->can)
(Let me know if I made a mistake on any of these examples or if you come up with more.)