1858. Longest Word With All Prefixes
https://leetcode.com/problems/longest-word-with-all-prefixes
Description
Given an array of strings words
, find the longest string in words
such that every prefix of it is also in words
.
For example, let
words = ["a", "app", "ap"]
. The string"app"
has prefixes"ap"
and"a"
, all of which are inwords
.
Return the string described above. If there is more than one string with the same length, return the lexicographically smallest one, and if no string exists, return ""
.
Example 1:
Example 2:
Example 3:
Constraints:
1 <= words.length <= 105
1 <= words[i].length <= 105
1 <= sum(words[i].length) <= 105
ac
Last updated