Find largest word in dictionary
Easy Accuracy: 33.04% Submissions: 2557 Points: 2

Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.

Example 1:

Input: d = {"ale", "apple", "monkey", "plea"}
       S = "abpcplea"
Output: "apple" 
Explanation: After deleting "b", "c"
"a" S became "apple" which is present
in d.

Example 2:

Input: d = {"a", "b", "c"}
       S = "abpcplea"
Output: "a"
Explanation: After deleting "b", "p"
"c", "p", "l", "e", "a" S became 
"a" which is present in d.

Your Task:  
You dont need to read input or print anything. Complete the function findLongestWord() which takes S and d as input parameter and returns the longest word

Expected Time Complexity: O(n*x)
Expected Auxiliary Space: O(x)

1 ≤ n,x ≤ 103

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Find largest word in dictionary

Output Window