Word chain referee
EasyOnline Assessment
Referee a word-chain game and find who makes the first mistake.
players people sit in a circle, numbered 1 to players, and take turns saying one word each, starting with player 1 (so player 1 says word 1, player 2 says word 2, and after player players it is player 1 again). words is the list of words in the order they were said.
A word is illegal if any of these is true:
- it has fewer than 3 letters;
- it is not the first word and its first letter differs from the last letter of the word said just before it;
- it has been said earlier in the game.
Capital letters never matter: Tree and tREE are the same word, and E equals e. The game stops at the first illegal word, so the previous word is always legal.
Input: words, a list of strings; players, an integer.
Output: [player, position] for the first illegal word, where player is the number of the player who said it and position is its 1-based place in words. If every word is legal (including when there are no words), return [].
Example 1
- Input:
- [["Lamp","pillow","WINDOW","wall"],3]
- Output:
- []
- Explanation
- Three players take turns. Every word starts with the last letter of the one before it (
Lampends in p,pillowstarts with p;pillowends in w andWINDOWstarts with W;WINDOWends in w andwallstarts with w). Capital letters do not matter, no word repeats, and every word has at least 3 letters, so nobody is out:[].
Example 2
- Input:
- [["stone","emu","urn","nest","tree","EMU","uncle"],2]
- Output:
- [2,6]
- Explanation
- Two players alternate.
EMUis the 6th word and was said by player 2, who already saidemuearlier; repeats ignore case. So the first illegal word is word 6 and the answer is[2, 6].
Example 3
- Input:
- [["gate","earth","tower","rose"],3]
- Output:
- [3,3]
- Explanation
- Three players:
gateandearthare fine, buttoweris the 3rd word, said by player 3, and starts with t whileearthends in h. Answer[3, 3].
Constraints
1 <= players <= 80 <= words.length <= 401 <= words[i].length <= 12, English letters only