Giving a dictionary and a string ‘str’, find the longest string in dictionary which can be formed by deleting some characters of the given ‘str’. Examples:
This problem reduces to finding if a string is subsequence of another string or not. We traverse all dictionary words and for every word, we check if it is subsequence of given string and is largest of all such words. We finally return the longest word with given string as subsequence. Below is the implementation of above idea
C++
// C++ program to find largest word in Dictionary
// by deleting some characters of given string
#include <bits/stdc++.h>
usingnamespacestd;
// Returns true if str1[] is a subsequence of str2[].
// m is length of str1 and n is length of str2
boolisSubSequence(string str1, string str2)
{
intm = str1.length(), n = str2.length();
intj = 0; // For index of str1 (or subsequence
// Traverse str2 and str1, and compare current
// character of str2 with first unmatched char
// of str1, if matched then move ahead in str1
for(inti = 0; i < n && j < m; i++)
if(str1[j] == str2[i])
j++;
// If all characters of str1 were found in str2
return(j == m);
}
// Returns the longest string in dictionary which is a
// Returns true if str1[] is a subsequence of str2[].
// m is length of str1 and n is length of str2
functionisSubSequence($str1, $str2)
{
$m= strlen($str1);
$n= strlen($str2);
$j= 0; // For index of str1 (or subsequence
// Traverse str2 and str1, and compare current
// character of str2 with first unmatched char
// of str1, if matched then move ahead in str1
for($i= 0; $i< $n&& $j< $m; $i++)
if($str1[$j] == $str2[$i])
$j++;
// If all characters of str1 were found in str2
return($j== $m);
}
// Returns the longest string in dictionary which is a
// subsequence of str.
functionfindLongestString($dict, $str)
{
$result= "";
$length= 0;
// Traverse through all words of dictionary
foreach($dictas$word)
{
// If current word is subsequence
// of str and is largest
// such word so far.
if($length< strlen($word) &&
isSubSequence($word, $str))
{
$result= $word;
$length= strlen($word);
}
}
// Return longest string
return$result;
}
// Driver code
$dict= array("ale", "apple", "monkey", "plea");
$str= "abpcplea";
echofindLongestString($dict, $str);
// This code is conribued by mits
?>
Javascript
<script>
// JavaScript program to find largest word in Dictionary
// by deleting some characters of given string
// Returns true if str1[] is a subsequence of str2[].
// m is length of str1 and n is length of str2
functionisSubSequence(str1, str2)
{
varm = str1.length, n = str2.length;
varj = 0; // For index of str1 (or subsequence
// Traverse str2 and str1, and compare current
// character of str2 with first unmatched char
// of str1, if matched then move ahead in str1
for(vari = 0; i < n && j < m; i++)
if(str1[j] == str2[i])
j++;
// If all characters of str1 were found in str2
return(j == m);
}
// Returns the longest string in dictionary which is a
// subsequence of str.
functionfindLongestString(dict, str)
{
varresult = "";
varlength = 0;
// Traverse through all words of dictionary
dict.forEach(word => {
// If current word is subsequence of str and is
// largest such word so far.
if(length < word.length
&& isSubSequence(word, str)) {
result = word;
length = word.length;
}
});
// Return longest string
returnresult;
}
// Driver program to test above function
vardict
= ["ale", "apple", "monkey", "plea"];
varstr = "abpcplea";
document.write( findLongestString(dict, str));
</script>
Output:
apple
Time Complexity: O(N*(K+n)) Here N is the length of dictionary and n is the length of given string ‘str’ and K – maximum length of words in the dictionary. Auxiliary Space: O(1)
An efficient solution is we Sort the dictionary word. We traverse all dictionary words and for every word, we check if it is subsequence of given string and at last we check this subsequence is largest of all such subsequence.. We finally return the longest word with given string as subsequence.
C++
// C++ program to find largest word in Dictionary
// by deleting some characters of given string
#include <bits/stdc++.h>
usingnamespacestd;
string res="";
voidcheck(string d,string s)
{
inti=0;
intj=0;
while(i<d.size() && j<s.size())
{
if(d[i]==s[j])
{
i++;
j++;
}
else
j++;
}
if(i==d.size() && res.size()<d.size())
{
res=d;
}
}
string LongestWord(vector<string> d,string S) {
//sort the dictionary word
// for smallest lexicographical order
sort(d.begin(),d.end());
for(string c:d)
{
check(c,S);
}
returnres;
}
// Driver program
intmain()
{
vector<string> dict
= { "ale", "apple", "monkey", "plea"};
string str = "abpcplea";
cout << LongestWord(dict, str) << endl;
return0;
}
Java
/*package whatever //do not write package name here */
# Python3 program to find largest word in Dictionary
# by deleting some characters of given string
res=""
defcheck(d,s):
globalres
i =0
j =0
while(i < len(d) andj < len(s)):
if(d[i] ==s[j]):
i +=1
j +=1
else:
j +=1
if(i ==len(d) andlen(res) < len(d)):
res =d
defLongestWord(d, S):
# sort the dictionary word
# for smallest lexicographical order
d.sort()
forc ind :
check(c,S)
returnres
# Driver program
dict=[ "ale", "apple", "monkey", "plea"]
str="abpcplea"
print(LongestWord(dict, str))
# This code is contributed by shinjanpatra
Javascript
<script>
// JavaScript program to find largest word in Dictionary
// by deleting some characters of given string
let res="";
functioncheck(d, s)
{
let i = 0;
let j = 0;
while(i < d.length && j < s.length)
{
if(d[i] == s[j])
{
i++;
j++;
}
else
j++;
}
if(i == d.length && res.length < d.length)
{
res = d;
}
}
functionLongestWord(d,S)
{
// sort the dictionary word
// for smallest lexicographical order
d.sort();
for(let c of d)
{
check(c, S);
}
returnres;
}
// Driver program
let dict = [ "ale", "apple", "monkey", "plea"];
let str = "abpcplea";
document.write(LongestWord(dict, str),"</br>");
// This code is contributed by shinjanpatra
</script>
This article is contributed by Nishant Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy