Find largest word in dictionary by deleting some characters of given string | Java 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 ; Traverse str2 and str1 , and compare current character of str2 with first unmatched char of str1 , if matched then move ahead in str1 ; If all characters of str1 were found in str2 ; Returns the longest String in dictionary which is a subsequence of str . ; Traverse through all words of dictionary ; If current word is subsequence of str and is largest such word so far . ; Return longest String ; Driver code
import java . util . * ; class GFG { static boolean isSubSequence ( String str1 , String str2 ) { int m = str1 . length ( ) , n = str2 . length ( ) ; 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 ) ) ; } }