Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Longest Word in Dictionary

Medium
triestringsortinghash-table

Given a list of words, return the longest word that can be built one character at a time, where every prefix of it (of every length) is also a word in the list. On ties, return the lexicographically smallest. Return an empty line if no word qualifies.

Input: one line of space-separated words. Output: the answer word (or empty).

Example 1
Input:
w wo wor worl world
Output:
world
Example 2
Input:
a banana app appl ap apply apple
Output:
apple

apple and apply tie at length 5; apple is smaller.

Example 3
Input:
abc bd
Output:

No single-letter starting point.

Constraints

  • 1 ≤ words.length ≤ 1000
  • 1 ≤ word length ≤ 30; lowercase letters.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.