Longest Word in Dictionary Problem

Longest Word in Dictionary Problem — ExecCode Medium DSA Practice

Solve the Longest Word in Dictionary problem on ExecCode. Free online medium DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given an array of strings words representing a dictionary, return the longest word that can be built one character at a time by other words in words. A word only "counts" if every one of its prefixes (dropping the last character repeatedly) is also present in the dictionary. If multiple words tie for longest, return the lexicographically smallest one.

Examples

Input words = ["w", "wo", "wor", "worl", "world"]; Output "world". Input words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]; Output "apple". Input words = ["ac", "ab", "a", "abc", "abd"]; Output "abc"

Constraints

1 <= words.length <= 1000 1 <= words[i].length <= 30 words[i] consists of lowercase English letters

Practice Longest Word in Dictionary free on ExecCode. Browse DSA problems, topic map, and placement guides.