Check if the given string of words can be formed from words present in the dictionary | Function to check if the word is in the dictionary or not ; map to store all words in dictionary with their count ; Adding all words in map ; Search in map for all words in the sentence ; All words of sentence are present ; Driver code ; Calling function to check if words are present in the dictionary or not
function match_words ( dictionary , sentence , n , m ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( dictionary [ i ] ) ) { mp . set ( dictionary [ i ] , mp . get ( dictionary [ i ] ) + 1 ) ; } else { mp . set ( dictionary [ i ] , 1 ) ; } } for ( let i = 0 ; i < m ; i ++ ) { if ( mp . has ( sentence [ i ] ) ) mp . set ( sentence [ i ] , mp . get ( sentence [ i ] ) - 1 ) ; else return false ; } return true ; } let dictionary = [ " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let n = dictionary . length ; let sentence = [ " " , " " , " " , " " , " " , " " , " " ] ; let m = sentence . length ; if ( match_words ( dictionary , sentence , n , m ) ) document . write ( " " ) ; else document . write ( " " ) ;