Ctrl K

Word Grown Letter by Letter

Medium
You are given a dictionary vocabulary, an array of words. A word can be *grown* if you can reach it by starting from a one-letter word and appending one letter at a time, with every intermediate word also present in the dictionary. In other words, every non-empty prefix of the word must be in the dictionary. Return the longest word in the dictionary that can be grown. If several such words share the maximum length, return the alphabetically smallest one. If no word can be grown, return the empty string.
Example 1
vocabulary=['s', 'st', 'sta', 'star', 'start', 'stars', 'tar'] → 'stars'

Both start and stars grow letter by letter: s → st → sta → star → … Both have length 5, and stars comes first alphabetically.

Example 2
vocabulary=['ox', 'oxen', 'b'] → 'b'

There is no one-letter o, so neither ox nor oxen can be built. Only b is left.

Example 3
vocabulary=['go', 'gone'] → ''

No word can be grown from a single letter, so the answer is the empty string.

Constraints
  • 1 ≤ len(vocabulary) ≤ 1000
  • 1 ≤ len(vocabulary[i]) ≤ 30
  • Words consist of lowercase English letters only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMost Searched Words MediumWords Hidden in a Text MediumAnagram Buckets MediumSplit Into Dictionary Words MediumA Subsequence of One Word Only Medium
loading Python…
Input
vocabulary=['s', 'st', 'sta', 'star', 'start', 'stars', 'tar']
Expected
'stars'

Run checks the first 3 tests in your browser. Submit checks all 117 tests on the server, hidden ones included.