191. Longest Word with All Prefixes

Given a string array nums of length n. A string is called a complete string if every prefix of this string is also present in the array nums.

Find the longest complete string in the array nums.

If there are multiple strings with the same length, return the lexicographically smallest one and if no string exists, return "None" (without quotes).

Example 1:

Input : nums = [ "n", "ni", "nin", "ninj" , "ninja" , "nil" ]

Output : ninja

Explanation :

The word "ninja" is the longest word which has all its prefixes present in the array.

Example 2:

Input : nums = [ "ninja" , "night" , "nil" ]

Output : None

Explanation :

There is no string that has all its prefix present in array. So we return None.

Now Your Turn!

Pick the correct output for the given input

Input : nums = [ "cat" , "car" , "cow", "c", "ca", "t", "r", "w" ]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 105
  • 1 <= nums[i].length <= 105
  • 1 <= sum(nums[i].length) <= 105
  • nums[i] consist only of lowercase English characters

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
string completeString(vector<string>& nums) {
//your code goes here
}
};
Test Case

Input:

Nums