Tap here to switch tabs
Problem
Submissions

Problem: Longest Word With All Prefixes

Longest Word With All Prefixes finds the longest string whose every nonempty prefix appears in the given array, returning the lexicographically smallest result on ties or an empty string if none qualifies.

Statement

You are given an array of strings, words. Your task is to find the longest string in words such that every prefix of this string is also present in words.

A prefix of a string is any leading substring. For example, the prefixes of "apple" are "a", "ap", "app", and "appl".

  1. If multiple valid strings have the same maximum length, return the lexicographically smallest one.

  2. If no such string exists, return an empty string "".

Constraints:

  • 1≤1 \leq words.length ≤103\leq 10^3

  • 1≤1 \leq words[i].length ≤103\leq 10^3

  • 1≤1 \leq sum(words[i].length) ≤103\leq 10^3

  • words[i] consists only of lowercase English letters.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Longest Word With All Prefixes

Longest Word With All Prefixes finds the longest string whose every nonempty prefix appears in the given array, returning the lexicographically smallest result on ties or an empty string if none qualifies.

Statement

You are given an array of strings, words. Your task is to find the longest string in words such that every prefix of this string is also present in words.

A prefix of a string is any leading substring. For example, the prefixes of "apple" are "a", "ap", "app", and "appl".

  1. If multiple valid strings have the same maximum length, return the lexicographically smallest one.

  2. If no such string exists, return an empty string "".

Constraints:

  • 1≤1 \leq words.length ≤103\leq 10^3

  • 1≤1 \leq words[i].length ≤103\leq 10^3

  • 1≤1 \leq sum(words[i].length) ≤103\leq 10^3

  • words[i] consists only of lowercase English letters.