Longest Word in Dictionary

Medium

Description

Given an array of strings words, return the longest word that can be built one character at a time by other words in the array.

Examples

Input:words = ["w","wo","wor","worl","world"]
Output:"world"
Explanation:

world built incrementally.

Input:words = ["cat","cats","dog","doggy","dogs","c","ca","d","do"]
Output:cats
Explanation:

Both 'cats' and 'dogs' can be built incrementally ('c'→'ca'→'cat'→'cats' and 'd'→'do'→'dog'→'dogs'), but 'cats' comes first alphabetically when lengths are equal.

Input:words = ["t","ti","tig","tiger","time","tim","te","test"]
Output:tiger
Explanation:

The word 'tiger' can be built step by step: 't'→'ti'→'tig'→'tiger'. Although 'test' has the same length, it cannot be built incrementally since 'tes' is missing from the word list.

Constraints

  • 1 ≤ words.length ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!