Find Longest Word
Requirements: completion of beginner software development
Given a string letters
and a set of words
, find the longest word in words
that is a subsequence of letters
.
Word W
is a subsequence of letters
if some
number of characters, possibly zero, can be deleted
from letters
to form W
, without reordering the
remaining characters. If no subsequence can be found, return an empty
string.
Example 1:
Input: words = [ "able", "ale", "apple", "bale", "kangaroo" ]
letters = "abppplee"
Output: apple
The words "able" and "ale" are both subsequences
of letters
, but they are shorter than "apple". The word
"bale" is not a subsequence of letters
because even
though letters
has all the right letters, they are not in
the right order. The word "kangaroo" is the longest word
in words
, but it isn't a subsequence
of letters
.
Example 2:
Input: nums = [ "mochi", "pudding", "tiramisu", "brulee", "macaron" ]
letters: "brunch"
Output: ""
letters
contains some of the characters in each of the
words, however it can't complete any of the words, thus no subsequence
found.
Example 3:
Input: nums = [ "diamond", "dia", "mond", "d", "diamon" ]
letters: "diamond"
Output:
All of the words are subsequences of letters
, the largest
being diamond
.
words:
letters:
Please try and complete this project before viewing the solution.