BackmediumStrings Oracle

Galactic Dictionary Solution

Problem Statement

Given a list of alien words, find the longest chain of words that follows the rule: each word must start with the last letter of the previous word. If multiple chains have the same maximum length, return the lexicographically smallest one.

Example 1
Input
['tachyon', 'nectar', 'yacht']
Output
['tachyon', 'nectar', 'yacht']

Explanation: Step-by-step: 1. Start with 'tachyon'. 2. Find a word that starts with 'N', the last letter of 'tachyon', which is 'nectar'. 3. Find a word that starts with 'R', the last letter of 'nectar', which is 'yacht'.

Example 2
Input
['starlight', 'tiger', 'ocean']
Output
['starlight', 'tiger', 'ocean']

Explanation: Step-by-step: 1. Start with 'starlight'. 2. Find a word that starts with 'T', the last letter of 'starlight', which is 'tiger'. 3. Find a word that starts with 'R', the last letter of 'tiger', which is 'ocean'.

Constraints

  • The length of the input list will not exceed 1000 words.
  • Each word in the list will have a length between 1 and 20 characters.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free