Digital Dictionary


how can i modify code so it will give “pet” and “peter” as output when prefix is “pe”, currenty it gives only “pet”

its simple , what you can do is recursively print all nodes under subtree of last matching node. ie. t in case of “pet” ,
I have explained through code with proper comments , you will get the idea of what i am talking

1 Like


after seeing your code, I edited my code and it works correctly for sample test case, but it is failing for all test case while submitting, please rectify the mistake in my code.

@dhairya16, if the prefix is not found in that case you have to insert the prefix into the trie,
so your dictionary function should be :-

void dictionary(string pre) {
	node*temp=findPrefix(pre);
	if(temp!=NULL) {
		printAllWords(temp,pre);
	}
	else {
        insert(pre);
		cout<<"No suggestions"<<endl;
	}
} 

corrected code :-

why to insert prefix in trie ?

@dhairya16, its given in question " And if no such words are available for a given search word, add this word to your dictionary."

1 Like

@O17LPOLA020023 ohh yes, I missed that, Thank you so much for amazing explanation :slightly_smiling_face: