A Trie is used to store dictionary words so that they can be searched efficiently and prefix search can be done. The task is to write a function to count the number of words.
Input : root / \ \ t a b | | | h n y | | \ | e s y e / | | i r w | | | r e e | r Output : 8 Explanation : Words formed in the Trie : "the", "a", "there", "answer", "any", "by", "bye", "their".
In Trie structure, we have a field to store end of word marker, we call it isLeaf in below implementation. To count words, we need to simply traverse the Trie and count all nodes where isLeaf is set.
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count the number of words with given prefix using Trie
- Sorting array of strings (or words) using Trie
- Sorting array of strings (or words) using Trie | Set-2 (Handling Duplicates)
- Check if the given Trie contains words starting from every alphabet
- Print all possible combinations of words from Dictionary using Trie
- Check if the given string of words can be formed from words present in the dictionary
- Trie | (Insert and Search)
- Trie | (Delete)
- Auto-complete feature using Trie
- Longest prefix matching - A Trie based solution in Java
- Pattern Searching using a Trie of all Suffixes
- Find shortest unique prefix for every word in a given list | Set 1 (Using Trie)
- Overview of Data Structures | Set 3 (Graph, Trie, Segment Tree and Suffix Tree)
- Longest Common Prefix using Trie
- Count of distinct substrings of a string using Suffix Trie
- Boggle | Set 2 (Using Trie)
- Trie memory optimization using hash map
- Trie | (Display Content)
- Word Break Problem | (Trie solution)
- Persistent Trie | Set 1 (Introduction)
Improved By : princiraj1992