252. Find Longest Word by Deleting Characters

MediumStringString

Given a string s and a dictionary of words, return the longest word in the dictionary that can be formed by deleting some characters of s (a subsequence). If multiple have the same maximum length, return the lexicographically smallest. If none, return the empty string. Input: JSON {s, dict}.

Input: JSON {s, dict}.

Output: Quoted string — the best matching word.

Examples

Example 1
Input: {"s":"abpcplea","dict":["ale","apple","monkey","plea"]}
Output: "apple"
Explanation: 'apple' is the longest subsequence.
Example 2
Input: {"s":"abpcplea","dict":["a","b","c"]}
Output: "a"
Explanation: Smallest among single chars.
Example 3
Input: {"s":"abc","dict":["d"]}
Output: ""
Explanation: No match.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →