Check if the given string of words can be formed from words present in the dictionary | Python3 program to check if a sentence can be formed from a given set of words . include < bits / stdc ++ . h > ; here isEnd is an integer that will store count of words ending at that node ; utility function to create a new node ; Initialize new node with null ; Function to insert new words in trie ; Iterate for the length of a word ; If the next key does not contains the character ; isEnd is increment so not only the word but its count is also stored ; Search function to find a word of a sentence ; Iterate for the complete length of the word ; If the character is not present then word is also not present ; If present move to next character in Trie ; If word found then decrement count of the word ; if the word is found decrement isEnd showing one occurrence of this word is already taken so ; Function to check if string can be formed from the sentence ; Iterate for all words in the string ; if a word is not found in a string then the sentence cannot be made from this dictionary of words ; If possible ; Function to insert all the words of dict in the Trie ; Driver Code ; Dictionary of words ; Calling Function to insert words of dictionary to tree ; String to be checked ; Function call to check possibility
ALPHABET_SIZE = 26 ; NEW_LINE class trieNode : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . t = [ None for i in range ( ALPHABET_SIZE ) ] NEW_LINE self . isEnd = 0 NEW_LINE DEDENT DEDENT def getNode ( ) : NEW_LINE INDENT temp = trieNode ( ) NEW_LINE return temp ; NEW_LINE DEDENT def insert ( root , key ) : NEW_LINE INDENT trail = None NEW_LINE trail = root ; NEW_LINE for i in range ( len ( key ) ) : NEW_LINE INDENT if ( trail . t [ ord ( key [ i ] ) - ord ( ' a ' ) ] == None ) : NEW_LINE INDENT temp = None NEW_LINE temp = getNode ( ) ; NEW_LINE trail . t [ ord ( key [ i ] ) - ord ( ' a ' ) ] = temp ; NEW_LINE DEDENT trail = trail . t [ ord ( key [ i ] ) - ord ( ' a ' ) ] ; NEW_LINE DEDENT ( trail . isEnd ) += 1 NEW_LINE DEDENT def search_mod ( root , word ) : NEW_LINE INDENT trail = root ; NEW_LINE for i in range ( len ( word ) ) : NEW_LINE INDENT if ( trail . t [ ord ( word [ i ] ) - ord ( ' a ' ) ] == None ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT trail = trail . t [ ord ( word [ i ] ) - ord ( ' a ' ) ] ; NEW_LINE DEDENT if ( ( trail . isEnd ) > 0 and trail != None ) : NEW_LINE INDENT ( trail . isEnd ) -= 1 NEW_LINE return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT def checkPossibility ( sentence , m , root ) : NEW_LINE INDENT flag = 1 ; NEW_LINE for i in range ( m ) : NEW_LINE INDENT if ( search_mod ( root , sentence [ i ] ) == False ) : NEW_LINE INDENT print ( ' NO ' , end = ' ' ) NEW_LINE return ; NEW_LINE DEDENT DEDENT print ( ' YES ' ) NEW_LINE DEDENT def insertToTrie ( dictionary , n , root ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT insert ( root , dictionary [ i ] ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = getNode ( ) ; NEW_LINE dictionary = [ " find " , " a " , " geeks " , " all " , " for " , " on " , " geeks " , " answers " , " inter " ] NEW_LINE N = len ( dictionary ) NEW_LINE insertToTrie ( dictionary , N , root ) ; NEW_LINE sentence = [ " find " , " all " , " answers " , " on " , " geeks " , " for " , " geeks " ] NEW_LINE M = len ( sentence ) NEW_LINE checkPossibility ( sentence , M , root ) ; NEW_LINE DEDENT