Find largest word in dictionary by deleting some characters of given string
Last Updated :
26 Apr, 2023
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:
Input : dict = {"ale", "apple", "monkey", "plea"}
str = "abpcplea"
Output : apple
Input : dict = {"pintu", "geeksfor", "geeksgeeks",
" forgeek"}
str = "geeksforgeeks"
Output : geeksgeeks
Asked In: Google Interview
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
#include <stdio.h>
#include <string.h>
int isSubSequence( char str1[], char str2[])
{
int m = strlen (str1), n = strlen (str2);
int j = 0;
for ( int i = 0; i < n && j < m; i++)
if (str1[j] == str2[i])
j++;
return (j == m);
}
char * findLongestString( char dict[][10], int size,
char str[])
{
char * result = "" ;
int length = 0;
for ( int i = 0; i < size; i++) {
char * word = dict[i];
if (length < strlen (word)
&& isSubSequence(word, str)) {
result = word;
length = strlen (word);
}
}
return result;
}
int main()
{
char dict[][10] = { "ale" , "apple" , "monkey" , "plea" };
char str[] = "abpcplea" ;
char * longestString = findLongestString(
dict, sizeof (dict) / sizeof (dict[0]), str);
printf ( "%s\n" , longestString);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
bool isSubSequence(string str1, string str2)
{
int m = str1.length(), n = str2.length();
int j = 0;
for ( int i = 0; i < n && j < m; i++)
if (str1[j] == str2[i])
j++;
return (j == m);
}
string findLongestString(vector<string> dict, string str)
{
string result = "" ;
int length = 0;
for (string word : dict) {
if (length < word.length()
&& isSubSequence(word, str)) {
result = word;
length = word.length();
}
}
return result;
}
int main()
{
vector<string> dict
= { "ale" , "apple" , "monkey" , "plea" };
string str = "abpcplea" ;
cout << findLongestString(dict, str) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isSubSequence(String str1,
String str2)
{
int m = str1.length(), n = str2.length();
int j = 0 ;
for ( int i = 0 ; i < n && j < m; i++)
{
if (str1.charAt(j) == str2.charAt(i))
{
j++;
}
}
return (j == m);
}
static String findLongestString(Vector<String> dict,
String str)
{
String result = "" ;
int length = 0 ;
for (String word : dict)
{
if (length < word.length() &&
isSubSequence(word, str))
{
result = word;
length = word.length();
}
}
return result;
}
public static void main(String[] args)
{
String[] arr = { "ale" , "apple" , "monkey" , "plea" };
Vector dict = new Vector(Arrays.asList(arr));
String str = "abpcplea" ;
System.out.println(findLongestString(dict, str));
}
}
|
Python3
def isSubSequence(str1, str2):
m = len (str1);
n = len (str2);
j = 0 ;
i = 0 ;
while (i < n and j < m):
if (str1[j] = = str2[i]):
j + = 1 ;
i + = 1 ;
return (j = = m);
def findLongestString(dict1, str1):
result = "";
length = 0 ;
for word in dict1:
if (length < len (word) and isSubSequence(word, str1)):
result = word;
length = len (word);
return result;
dict1 = [ "ale" , "apple" , "monkey" , "plea" ];
str1 = "abpcplea" ;
print (findLongestString(dict1, str1));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool isSubSequence(String str1,
String str2)
{
int m = str1.Length, n = str2.Length;
int j = 0;
for ( int i = 0; i < n && j < m; i++)
{
if (str1[j] == str2[i])
{
j++;
}
}
return (j == m);
}
static String findLongestString(List<String> dict,
String str)
{
String result = "" ;
int length = 0;
foreach (String word in dict)
{
if (length < word.Length &&
isSubSequence(word, str))
{
result = word;
length = word.Length;
}
}
return result;
}
public static void Main(String[] args)
{
String[] arr = { "ale" , "apple" , "monkey" , "plea" };
List<String> dict = new List<String>(arr);
String str = "abpcplea" ;
Console.WriteLine(findLongestString(dict, str));
}
}
|
PHP
<?php
function isSubSequence( $str1 , $str2 )
{
$m = strlen ( $str1 );
$n = strlen ( $str2 );
$j = 0;
for ( $i = 0; $i < $n && $j < $m ; $i ++)
if ( $str1 [ $j ] == $str2 [ $i ])
$j ++;
return ( $j == $m );
}
function findLongestString( $dict , $str )
{
$result = "" ;
$length = 0;
foreach ( $dict as $word )
{
if ( $length < strlen ( $word ) &&
isSubSequence( $word , $str ))
{
$result = $word ;
$length = strlen ( $word );
}
}
return $result ;
}
$dict = array ( "ale" , "apple" , "monkey" , "plea" );
$str = "abpcplea" ;
echo findLongestString( $dict , $str );
?>
|
Javascript
<script>
function isSubSequence(str1, str2)
{
var m = str1.length, n = str2.length;
var j = 0;
for ( var i = 0; i < n && j < m; i++)
if (str1[j] == str2[i])
j++;
return (j == m);
}
function findLongestString(dict, str)
{
var result = "" ;
var length = 0;
dict.forEach(word => {
if (length < word.length
&& isSubSequence(word, str)) {
result = word;
length = word.length;
}
});
return result;
}
var dict
= [ "ale" , "apple" , "monkey" , "plea" ];
var str = "abpcplea" ;
document.write( findLongestString(dict, str));
</script>
|
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
#include <stdio.h>
#include <string.h>
int isSubSequence( char * str1, char * str2)
{
int m = strlen (str1);
int n = strlen (str2);
int j = 0;
for ( int i = 0; i < n && j < m; i++)
if (str1[j] == str2[i])
j++;
return (j == m);
}
char * findLongestString( char ** dict, int n, char * str)
{
char * result = "" ;
int length = 0;
for ( int k = 0; k < n; k++) {
char * word = dict[k];
if (length < strlen (word)
&& isSubSequence(word, str)) {
result = word;
length = strlen (word);
}
}
return result;
}
int main()
{
char * dict[] = { "ale" , "apple" , "monkey" , "plea" };
int n = sizeof (dict) / sizeof (dict[0]);
char str[] = "abpcplea" ;
printf ( "%s\n" , findLongestString(dict, n, str));
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
string res= "" ;
void check(string d,string s)
{
int i=0;
int j=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(d.begin(),d.end());
for (string c:d)
{
check(c,S);
}
return res;
}
int main()
{
vector<string> dict
= { "ale" , "apple" , "monkey" , "plea" };
string str = "abpcplea" ;
cout << LongestWord(dict, str) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static String res= "" ;
static void check(String d,String s){
int i = 0 ;
int j = 0 ;
while (i < d.length() && j < s.length()){
if (d.charAt(i) == s.charAt(j)){
i += 1 ;
j += 1 ;
}
else
j += 1 ;
if (i == d.length() && res.length() < d.length()){
res = d;
}
}
}
static String LongestWord(ArrayList<String>d,String S){
Collections.sort(d);
for (String c:d){
check(c,S);
}
return res;
}
public static void main(String args[]){
ArrayList<String>dict = new ArrayList<String>(Arrays.asList( "ale" , "apple" , "monkey" , "plea" ));
String str = "abpcplea" ;
System.out.println(LongestWord(dict, str));
}
}
|
Python3
res = ""
def check(d,s):
global res
i = 0
j = 0
while (i < len (d) and j < len (s)):
if (d[i] = = s[j]):
i + = 1
j + = 1
else :
j + = 1
if (i = = len (d) and len (res) < len (d)):
res = d
def LongestWord(d, S):
d.sort()
for c in d :
check(c,S)
return res
dict = [ "ale" , "apple" , "monkey" , "plea" ]
str = "abpcplea"
print (LongestWord( dict , str ))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
static string res = "" ;
static void check( string d, string s){
int i = 0;
int j = 0;
while (i < d.Length && j < s.Length){
if (d[i] == s[j]){
i += 1;
j += 1;
}
else
j += 1;
if (i == d.Length && res.Length < d.Length){
res = d;
}
}
}
static string LongestWord(List< string > d, string S){
d.Sort();
foreach ( string c in d){
check(c, S);
}
return res;
}
public static void Main( string [] args){
List< string > dict = new List< string >( new string [] { "ale" , "apple" , "monkey" , "plea" });
string str = "abpcplea" ;
Console.WriteLine(LongestWord(dict, str));
}
}
|
Javascript
<script>
let res= "" ;
function check(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;
}
}
function LongestWord(d,S)
{
d.sort();
for (let c of d)
{
check(c, S);
}
return res;
}
let dict = [ "ale" , "apple" , "monkey" , "plea" ];
let str = "abpcplea" ;
document.write(LongestWord(dict, str), "</br>" );
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...